PS: Best Practices Referring Environment Variables in Powershell Programming

  1. A very good practice to retrieve environment variables values is to refer them as scalar values via $env context
  2.  

    PS C:> echo $env:appdata
    C:UsersgovardhanAppDataRoaming
    PS C:> write-host $env:appdata
    C:UsersgovardhanAppDataRoaming
    PS C:>

  3. If you are in practice of using Get-Item to retrieve environment variables, be cautious on possible typo of using $Env which would fail as shown below, so don’t use $Env while retrieving environment variables via Get-Item.

     

    PS C:> Get-Item Env:UserName

    Name                           Value
    —-                           —–
    USERNAME                       Govardhan

    PS C:> Get-Item $Env:UserName
    Get-Item : Cannot find path ‘C:Govardhan’ because it does not exist.
    At line:1 char:9
    + Get-Item <<<<  $Env:UserName
        + CategoryInfo          : ObjectNotFound: (C:Govardhan:String) [Get-Item]
       , ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetIt
       emCommand

    PS C:>

  4. Also if you use Get-Item to read environment variables, remember that the returned value is an object and you need to access the it’s value property to as shown below
  5.  

    PS C:> $a = Get-Item Env:Appdata
    PS C:> Write-Host
    $a
    System.Collections.DictionaryEntry
    PS C:> $a = Get-Item Env:Appdata
    PS C:> Write-Host $a.Value
    C:UsersgovardhanAppDataRoaming
    PS C:>

  6. Referring Env variables in scalar context as $Env converts the statement into a PS command.  You can directly access environment variable values via $env:<variablename> as shown below                                                                                                                    PS C:> $env:username
    Govardhan
    PS C:>
  7. Yet more better way to deal coding clarity (no typos with $env and Env) with Environment variable is to use the GetEnvironmentVariable and SetEnvironmentVariable

     

    Retrieving Machine level environment variables:

    PS C:> $a = [Environment]::GetEnvironmentVariable("windir","Machine")
    PS C:> echo $a
    C:Windows

    PS C:>

    Retrieving User level environment variables:

    PS C:> $a = [Environment]::GetEnvironmentVariable("Temp","User")
    PS C:> echo $a
    C:UsersgovardhanAppDataLocalTemp
    PS C:>

  8. .

0 thoughts on “PS: Best Practices Referring Environment Variables in Powershell Programming

  1. You are only sort of correct. Yes, the Get-Item expression will fail if you try to use $env:username because that is not a path. But you can use $env:username by itself without needing Get-Item. Think of the $ sign as turning env:username into a variable. This means you can easily do something like this:

    write “Logged on as $env:username on $env:computername”

    So I would argue forgo using Get-Item, and use the values as variables directly. Both of these return the same value.

    PS C:> (get-item env:username).value
    PS C:> $env:username

    I’ll pick the latter for simplicity and clarity.

Leave a Reply

Your email address will not be published. Required fields are marked *