首页 > 解决方案 > PowerShell:什么样的关键字 ist env: ?这样一个关键字的含义?

问题描述

要在 PowerShell 中显示所有环境变量,请使用:

Get-ChildItem env:

什么是'env:'?

我很清楚它是“环境”的缩写。但是什么样的缩写呢?最后的冒号是什么意思?

标签: powershellpowershell-remoting

解决方案


您的查询在 PowerShell 帮助文件中定义:

关于自动变量:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.1

$env 是上述的虚拟驱动器。通过以下内容很容易看到,其目的是允许访问上述内容:

# These are treated as normal filesystem drives, and you can create custom ones.
Get-PSDrive
<#
Name     Used (GB) Free (GB) Provider    Root               CurrentLocation
----     --------- --------- --------    ----               ---------------
Alias                        Alias                                         
C             5.04     34.83 FileSystem  C:\                        Scripts
Cert                         Certificate \                                 
Env                          Environment                                   
Function                     Function                                      
HKCU                         Registry    HKEY_CURRENT_USER                 
HKLM                         Registry    HKEY_LOCAL_MACHINE                
Variable                     Variable                                      
WSMan                        WSMan  
#>

Get-ChildItem -Path 'env:\'
<#
Using these in code requires the $ in front of each named variable to get the content/values. In your scripts, never name your custom variables the same as any of the below. 

Name                            Value                                                                                                                 
----                            -----                                                                                                                 
ALLUSERSPROFILE                 C:\ProgramData                                                                                                        
APPDATA                         C:\Users\WDAGUtilityAccount\AppData\Roaming                                                                           
CLIENTNAME                      8fda9520-99a6-4                                                                                                       
CommonProgramFiles              C:\Program Files\Common Files                                                                                         
CommonProgramFiles(x86)         C:\Program Files (x86)\Common Files                                                                                   
CommonProgramW6432              C:\Program Files\Common Files                                                                                         
COMPUTERNAME                    0C092C31-6890-4
...  
#>

如果您确实想使用上面的名称作为其他值的自定义变量,那么您应该提供一个唯一的前缀。说,您的姓名缩写,以免发生冲突/错误。


推荐阅读