$env vs env: vs [environment] -- best practice?

Welcome Forums General PowerShell Q&A $env vs env: vs [environment] -- best practice?

This topic contains 2 replies, has 3 voices, and was last updated by

5 years, 6 months ago.

  • Author
  • #13173

    Topics: 2
    Replies: 0
    Points: 21
    Rank: Member

    Is there a "best practice" for getting environment values such as the local computer name?

    I've found each of the following work, and I've found web sites that recommend each one, but I've no idea which would be considered a "best practice".


    get-content env:computername


  • #13174

    Topics: 0
    Replies: 669
    Points: 0
    Rank: Member

    I tend to use $env:COMPUTERNAME because its less typing and tab completion works

  • #13175

    Topics: 9
    Replies: 2322
    Points: 0
    Rank: Member

    I don't know about best practices, but I mostly like $env:COMPUTERNAME for its convenience; it can be embedded directly in a double-quoted string without the need for subexpressions.

    That said, if you specifically want to work with a System environment variable instead of a Process variable, you'll need to use methods in the [System.Environment] class. For example, these two commands will almost certainly return different values:

    [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine)

The topic ‘$env vs env: vs [environment] -- best practice?’ is closed to new replies.