显示运行Powershell脚本的所有环境变量

时间:2022-01-30 22:57:22

I need to display all configured environment variables in a Powershell script at runtime. Normally when displaying envvars I can just use one of the following at the shell (amongst other techniques, but these are simple):

我需要在运行时在Powershell脚本中显示所有已配置的环境变量。通常在显示envvars时我可以在shell中使用以下其中一种(除了其他技术,但这些很简单):

gci env:*
ls Env:

However, I have a script being called from another program, and when I use one of the above calls in the script, instead of being presented with envvars and their values, I instead get a list of System.Collections.DictionaryEntry types instead of the variables and their values. Inside of a Powershell script, how can I display all environment variables?

但是,我从另一个程序调用了一个脚本,当我在脚本中使用上述调用之一时,而不是显示envvars及其值,而是获取System.Collections.DictionaryEntry类型的列表而不是变量及其值。在Powershell脚本中,如何显示所有环境变量?

3 个解决方案

#1


43  

shorter version: gci env:* | sort-object name

更短版本:gci env:* |排序对象名称

#2


12  

Finally fumbled my way into a solution by iterating over each entry in the dictionary:

最后通过迭代字典中的每个条目来摸索我的方式:

(gci env:*).GetEnumerator() | Sort-Object Name | Out-String

(gci env:*)。GetEnumerator()|排序对象名称|输出字符串

#3


6  

Shortest version (with variables sorted by name):

最短版本(变量按名称排序):

gci env:

#1


43  

shorter version: gci env:* | sort-object name

更短版本:gci env:* |排序对象名称

#2


12  

Finally fumbled my way into a solution by iterating over each entry in the dictionary:

最后通过迭代字典中的每个条目来摸索我的方式:

(gci env:*).GetEnumerator() | Sort-Object Name | Out-String

(gci env:*)。GetEnumerator()|排序对象名称|输出字符串

#3


6  

Shortest version (with variables sorted by name):

最短版本(变量按名称排序):

gci env: