How to print environment variables to the console

2020-05-14 13:13发布

I'm starting to use PowerShell and am trying to figure out how to echo a system environment variable to the console to read it.

Neither of the below are working. The first just prints %PATH%, and the second prints nothing.

echo %PATH%
echo $PATH

标签: powershell
2条回答
兄弟一词,经得起流年.
2楼-- · 2020-05-14 13:43

In addition to Mathias answer.

Although not mentioned in OP, if you also need to see the Powershell specific/related internal variables, you need to use Get-Variable:

$ Get-Variable

Name                           Value
----                           -----
$                              name
?                              True
^                              gci
args                           {}
ChocolateyTabSettings          @{AllCommands=False}
ConfirmPreference              High
DebugPreference                SilentlyContinue
EnabledExperimentalFeatures    {}
Error                          {System.Management.Automation.ParseException: At line:1 char:1...
ErrorActionPreference          Continue
ErrorView                      NormalView
ExecutionContext               System.Management.Automation.EngineIntrinsics
false                          False
FormatEnumerationLimit         4
...

These also include stuff you may have set in your profile startup script.

查看更多
够拽才男人
3楼-- · 2020-05-14 13:57

Prefix the variable name with env:

$env:path

You can also enumerate all variables via the env drive:

Get-ChildItem env:
查看更多
登录 后发表回答