是否有PowerShell命令可列出所有先前加载的变量?
我正在Visual Studio中运行一些PowerShell脚本,希望列出当前PowerShell会话中可用的所有变量。
我试过这个命令:
ls variable:*;
但它返回以下内容:
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
3条答案
按热度按时间wi3ka0sx1#
ls variable:*
应该可以工作,或者Get-Variable
。如果这些导致了错误的输出,那是由于一个实现很差的主机,而不是powershell本身。如果你打开标准的控制台主机(运行powershell.exe),你会看到这些工作正常。如果您需要绕过坏主机,那么将所有内容转储到显式字符串可能会更好:
或
dgsult0t2#
有趣的是,您可以只输入
variable
,这也可以工作!我发现这个是因为我很好奇
ls variable:*
在做什么。Get-Help ls
告诉我们它是PowerShell的Get-ChildItem的别名,我知道它会列出一个对象的所有子对象。所以,我只尝试了variable
,瞧!基于this和this,
ls variable:*
所做的似乎是告诉它使用variable
列表上的*
(all/any)通配符进行某种范围/名称空间查找,在本例中,通配符似乎是无关紧要的(ls variable:*
==ls variable:
==variable
)。x0fgdtte3#
我经常东游西荡,在交互式环境中测试概念,但有时会忘记变量名或我定义的所有内容。
下面是我编写的一个用于 Package
Get-Variable
的函数,它自动排除shell启动时定义的任何全局变量:将下面一行放在profile.ps1文件的某个地方,或者放在最顶部,或者放在最底部,这取决于您是否希望自动排除每次运行交互式环境时定义的任何其他变量:
希望这是有帮助的!