Search code examples
powershellpowershell-remoting

PowerShell: What kind of keyword ist env: ? Meaning of such a keyword?


For to display all environmental variables in PowerShell one uses:

Get-ChildItem env:

What is 'env:'?

It clear to me that it is some abbreviation for "environment". But what kind of abbreviation? What's the meaning of the colon at the end?


Solution

  • Your query is defined in the PowerShell help files:

    About Automatic Variables:

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

    $env is a virtual drive for the above. Easily seen via the below and its purpose to allow access to the above:

    # 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
    ...  
    #>
    

    If you do want to use the names above, as custom variables for other values, then you should provide a unique prefix. Say, your initials, so as to not have conflicts/errors.