如何在PowerShell中将环境变量打印到控制台?


Answers:


175

在变量名前添加env

$env:path

您也可以通过env驱动器枚举所有变量:

Get-ChildItem env:

21
我喜欢$Env:Path.Split(';')我自己(每行输出一个目录)。
Bill_Stewart '19

1
当然可以,或者$env:Path -split ';'
Mathias R. Jessen

1
@AmirKatz输出是相同的,因为两个操作执行的操作完全相同:) -split是正则表达式运算符,String.Split()不是
Mathias R. Jessen

3
短期使用gci env:
navigaid

6

除了Mathias的答案。

尽管OP中未提及,但如果您还需要查看Powershell特定/相关的内部变量,则需要使用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
...

这些还包括您可能在配置文件启动脚本中设置的内容。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.