Display all environment variables from a running PowerShell script

别说谁变了你拦得住时间么 提交于 2020-01-30 14:03:31

问题


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

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 environment variables 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?


回答1:


Shorter version:

gci env:* | sort-object name

This will display both the name and value.




回答2:


Shortest version (with variables sorted by name):

gci env:



回答3:


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

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



回答4:


Short version with a wild card filter:

gci env: | where name -like 'Pro*'


来源:https://stackoverflow.com/questions/39800481/display-all-environment-variables-from-a-running-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!