Environment

PROVIDER NAME
    Environment

DRIVES
    Env:

SYNOPSIS
    Provides access to the Windows Environment Variables.

DESCRIPTION
    The Windows PowerShell Environment provider lets you get, add, change, clear, and delete Windows Environment Variables in Windows PowerShell.

    The Environment provider is a flat namespace that contains only objects that represent the Environment Variables. The Variables have no child items.

    Each Environment Variable is an instance of the System.Collections.DictionaryEntry class. The name of the Variable is the dictionary key. The value of the Environment Variable is the dictionary value.

    The Environment provider exposes its data store in the Env: drive. To work with Environment Variables, change your location to the Env: drive (“Set-Location env:”), or work from another Windows PowerShell drive. To reference an Environment Variable from another location, use the Env: drive name in the path.

    The Environment Variable provider supports all the cmdlets that contain the Item noun except for Invoke-Item. And, it supports the Get-Content and Set-Content cmdlets. However, it does not support the cmdlets that contain the ItemProperty noun, and it does not support the Filter parameter in any cmdlet.

    Environment Variables must conform to the usual naming standards. Additionally, the name cannot include the equal sign (=).

    Changes to the Environment Variables affect the current session only. To save the changes, add the changes to the Windows PowerShell profile, or use Export-Console to save the current session.

CAPABILITIES

TASKS
    TASK: Getting to the Env: Drive

        ————————– EXAMPLE 1 ————————–

        This command changes the current location to the Env: drive:

        Set-Location env:

        You can use this command from any drive in Windows PowerShell. To return to a file system drive, type the drive name. For example, type:

            Set-Location c:

    TASK: Getting Environment Variables

        ————————– EXAMPLE 1 ————————–

        This command lists all the Environment Variables in the current session:

        Get-ChildItem -path env:

        You can use this command from any Windows PowerShell drive.
        ————————– EXAMPLE 2 ————————–

        This command gets the WINDIR Environment Variable:

        Get-ChildItem -path env:windir

        ————————– EXAMPLE 3 ————————–

        This command gets a list of all the Environment Variables in the current session and then sorts them by name:

        Get-ChildItem | Sort-Object -property name

        By default, the Environment Variables appear in the order that Windows PowerShell discovers them. This command is submitted in the Env: drive.

        When you run this command from another drive, add the Path parameter with a value of Env:.

    TASK: Creating a New Environment Variable

        ————————– EXAMPLE 1 ————————–

        This command creates the USERMODE Environment Variable with a value of Non-Admin:

        New-Item -path . -name USERMODE -value Non-Admin

        Because the current location is in the Env: drive, the value of the Path parameter is a dot (.). The dot represents the current location.

        If you are not in the Env: drive, the value of the Path parameter would be Env:.

    TASK: Displaying the Properties and Methods of Environment Variables

        ————————– EXAMPLE 1 ————————–

        This command uses the Get-ChildItem cmdlet to get all the Environment Variables:

        Get-ChildItem -path env: | Get-Member

        The pipeline operator (|) sends the results to Get-Member, which displays the methods and properties of the object.

        When you pipe a collection of objects to Get-Member, such as the collection of Environment Variables in the Env: drive, Get-Member evaluates each object in the collection separately. Get-Member then returns information about each object type that it finds. If all the objects are of the same type, it returns information about the single object type. In this case, all the Environment Variables are DictionaryEntry objects.

        To get information about the collection of DictionaryEntry objects, use the InputObject parameter of Get-Member. For example, type:

            Get-Member -inputobject (Get-ChildItem env:)

        When you use the InputObject parameter, Get-Member evaluates the collection, not the objects in the collection.
        ————————– EXAMPLE 2 ————————–

        This command lists the values of the properties of the WINDIR Environment Variable:

        Get-Item env:windir | Format-List -property *

        It uses the Get-Item cmdlet to get an object that represents the WINDIR Environment Variable. The pipeline operator (|) sends the results to the Format-List command. It uses the Property parameter with a wildcard character (*) to format and display the values of all the properties of the WINDIR Environment Variable.

    TASK: Changing the Properties of an Environment Variable

        ————————– EXAMPLE 1 ————————–

        This command uses the Rename-Item cmdlet to change the name of the USERMODE Environment Variable that you created to USERROLE:

        Rename-Item -path env:USERMODE -newname USERROLE

        This change affects the Name, Key, and PSPath properties of the DictionaryEntry object.

        Do not change the name of an Environment Variable that the system uses. Although these changes affect only the current session, they might cause the system or a program to operate incorrectly.
        ————————– EXAMPLE 2 ————————–

        This command uses the Set-Item cmdlet to change the value of the USERROLE Environment Variable to Administrator:

        Set-Item -path env:USERROLE -value Administrator

    TASK: Copying an Environment Variable

        ————————– EXAMPLE 1 ————————–

        This command copies the value of the USERROLE Environment Variable to the USERROLE2 Environment Variable:

        Copy-Item -path env:USERROLE -destination env:USERROLE2

    TASK: Deleting an Environment Variable

        ————————– EXAMPLE 1 ————————–

        This command deletes the USERROLE2 Environment Variable from the current session:

        Remove-Item -path env:USERROLE2

        You can use this command in any Windows PowerShell drive. If you are in the Env: drive, you can omit the drive name from the path.
        ————————– EXAMPLE 2 ————————–

        This command deletes the USERROLE Environment Variable.

        Clear-Item -path env:USERROLE

DYNAMIC PARAMETERS

NOTES
    The Environment provider does not support any dynamic parameters.

RELATED LINKS
    about_providers