列出所有以前加载的PowerShell变量

Zoo*_*ing 29 powershell powershell-2.0

是否有PowerShell命令列出所有以前加载的变量?

我在Visual Studio中运行一些PowerShell脚本,并希望列出当前PowerShell会话中可用的所有变量.

我试过这个命令:

ls variable:*;
Run Code Online (Sandbox Code Playgroud)

但它返回以下内容:

System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
Run Code Online (Sandbox Code Playgroud)

lat*_*kin 50

ls variable:*应该工作,或Get-Variable.如果这些导致输出不良,则是由于主机实现不佳,而不是PowerShell本身.如果您打开标准控制台主机(运行powershell.exe),您将看到这些工作正常.

如果你需要解决一个糟糕的主机,你可能会更好地将所有内容转储到显式字符串:

Get-Variable | Out-String
Run Code Online (Sandbox Code Playgroud)

要么

Get-Variable |%{ "Name : {0}`r`nValue: {1}`r`n" -f $_.Name,$_.Value }
Run Code Online (Sandbox Code Playgroud)


And*_*rew 5

有趣的是,您可以只输入variable,这也可以!

我想通了,因为我对ls variable:*正在做的事情感到好奇。Get-Help ls告诉我们这是PowerShell的Get-ChildItem的别名,我知道它将列出对象的所有子项。因此,我尝试了variable,瞧!

基于此操作似乎ls variable:*是在告诉它使用列表中的*(所有/任意)通配符进行某种范围/名称空间查找variable,在这种情况下,这似乎是多余的(ls variable:*== ls variable:== variable) 。