about_environment_variables

TOPIC
    about_environment_variables

SHORT DESCRIPTION
    Describes how to access Windows Environment Variables in Windows
    PowerShell.

LONG DESCRIPTION
    Environment Variables store information about the operating system
    Environment. This information includes details such as the operating
    system path, the number of processors used by the operating system,
    and the location of temporary folders.

    The Environment Variables store data that is used by the operating system
    and other programs. For example, the WINDIR Environment Variable
    contains the location of the Windows installation directory. Programs
    can query the value of this Variable to determine where Windows operating
    system files are located.

    Windows PowerShell lets you view and change Windows Environment Variables,
    including the Variables set in the Registry, and those set for a particular
    session. The Windows PowerShell Environment provider simplifies this process
    by making it easy to view and change the Environment Variables.

    Unlike other types of Variables in Windows PowerShell, Environment Variables
    and their values are inherited by child sessions, such as local background
    jobs and the sessions in which module members run. This makes Environment
    Variables well suited to storing values that are needed in both parent and
    child sessions.

Windows PowerShell Environment Provider
     The Windows PowerShell Environment provider lets you access Windows
     Environment Variables in Windows PowerShell in a Windows PowerShell drive
     (the Env: drive). This drive looks much like a file system drive. To go
     to the Env: drive, type:

     Set-Location env:

     Then, to display the contents of the Env: drive, type:

     Get-ChildItem

     You can view the Environment Variables in the Env: drive from any other
     Windows PowerShell drive, and you can go into the Env: drive to view and
     change the Environment Variables.

Environment Variable Objects
     In Windows PowerShell, each Environment Variable is represented by an
     object that is an instance of the System.Collections.DictionaryEntry
     class.

     In each DictionaryEntry object, the name of the Environment Variable
     is the dictionary key. The value of the Variable is the dictionary
     value.

     To display an Environment Variable in Windows PowerShell, get an object
     that represents the Variable, and then display the values of the object
     properties. When you change an Environment Variable in Windows
     PowerShell, use the methods that are associated with the DictionaryEntry
     object.

     To display the properties and methods of the object that represents an
     Environment Variable in Windows PowerShell, use the Get-Member cmdlet.
     For example, to display the methods and properties of all the objects
     in the Env: drive, type:

     Get-Item -path env:* | Get-Member

Displaying Environment Variables
     You can use the cmdlets that contain the Item noun (the Item cmdlets) to
     display and change the values of Environment Variables. Because
     Environment Variables do not have child items, the output of Get-Item
     and Get-ChildItem is the same.

     When you refer to an Environment Variable, type the Env: drive name
     followed by the name of the Variable. For example, to display the value
     of the COMPUTERNAME Environment Variable, type:

     Get-ChildItem env:computername

     To display the values of all the Environment Variables, type:

     Get-ChildItem env:

     By default, Windows PowerShell displays the Environment Variables in the
     order in which it retrieves them. To sort the list of Environment
     Variables by Variable name, pipe the output of a Get-ChildItem command to
     the Sort-Object cmdlet. For example, from any Windows PowerShell drive,
     type:

     Get-ChildItem env: | sort name

     You can also go into the Env: drive by using the Set-Location cmdlet:

     Set-Location env:

     When you are in the Env: drive, you can omit the Env: drive name from
     the path. For example, to display all the Environment Variables, type:

     Get-ChildItem

     To display the value of the COMPUTERNAME Variable from within the
     Env: drive, type:

     Get-ChildItem computername

     You can also display and change the values of Environment Variables
     without using a cmdlet by using the expression parser in Windows
     PowerShell. To display the value of an Environment Variable, use the
     following syntax:

     $env:<variable-name>

     For example, to display the value of the WINDIR Environment Variable,
     type the following command at the Windows PowerShell command prompt:

     $env:windir

     In this syntax, the dollar sign ($) indicates a Variable, and the drive
     name indicates an Environment Variable.

Changing Environment Variables
     To make a persistent change to an Environment Variable, use System in
     Control Panel (Advanced tab or the Advanced System Settings item) to
     store the change in the Registry.

     When you change Environment Variables in Windows PowerShell, the change
     affects only the current session. This behavior resembles the behavior
     of the Set command in Windows-based Environments and the Setenv command
     in UNIX-based Environments.

     You must also have permission to change the values of the Variables. If
     you try to change a value without sufficient permission, the command
     fails, and Windows PowerShell displays an error.

     You can change the values of Variables without using a cmdlet by using
     the following syntax:

         $env:<variable-name> = “<new-value>”

     For example, to append “;c:\temp” to the value of the Path
     Environment Variable, use the following syntax:

     $env:path = $env:path + “;c:\temp”

     You can also use the Item cmdlets, such as Set-Item, Remove-Item, and
     Copy-Item to change the values of Environment Variables. For example, to
     use the Set-Item cmdlet to append “;c:\temp” to the value of the Path
     Environment Variable, use the following syntax:

         Set-Item -path env:path -value ($env:path + “;c:\temp”)

     In this command, the value is enclosed in parentheses so that it is
     interpreted as a unit.

Saving Changes to Environment Variables
     To create or change the value of an Environment Variable in every
     Windows PowerShell session, add the change to your Windows PowerShell
     profile.

     For example, to add the C:\Temp directory to the Path Environment
     Variable in every Windows PowerShell session, add the following
     command to your Windows PowerShell profile.

     $env:path = $env:path + “;c:\temp”

     To add the command to an existing profile, such as the CurrentUser,AllHosts
     profile, type:

     Add-Content -path $profile.CurrentUserAllHosts -value ‘$env:path = $env:path + “;c:\temp”‘

Environment Variables That Store Preferences
     Windows PowerShell features can use Environment Variables to store user
     preferences. These Variables work like preference Variables, but they
     are inherited by child sessions of the sessions in which they are created.
     For more information about preference Variables, see about_preference_variables.

     The Environment Variables that store preferences include:

        PSModulePath
            Stores the paths to the default module directories. Windows PowerShell
            looks for modules in the specified directories when you do not specify
            a full path to a module.

            The default value of $env:PSModulePath is:

                $home\Documents\WindowsPowerShell\Modules; $pshome\Modules

            Windows PowerShell sets the value of “$pshome\Modules” in the Registry.
            It sets the value of “$home\Documents\WindowsPowerShell\Modules” each
            time you start Windows PowerShell.

            In addition, setup programs that install modules in other directories,
            such as the Program Files directory, append their locations to the
            value of PSModulePath.

            To change the default module directories, change the value of the
            PSModulePath Environment Variable.

            For example, to add the “C:\ps-test\Modules” directory to the value
            of the PSModulePath Environment Variable, type:

                $env:PSModulePath = $env:PSModulePath + “;c:\ps-test\Modules”

            The semi-colon (;) in the command separates the new path from the
            path that precedes it in the list.

            Your changes affect only the current session unless you add a
            command that changes the value to your profile or use System in
            Control Panel to change the value of the PSModulePath Environment
            Variable in the Registry.

            For more information, see about_modules.

SEE ALSO
    Environment (provider)