Variable

PROVIDER NAME
    Variable

DRIVES
    Variable:

SYNOPSIS
    Provides access to the Windows PowerShell Variables and to their values.

DESCRIPTION
    The Windows PowerShell Variable provider lets you get, add, change, clear, and delete Windows PowerShell Variables in the current console.

    The Windows PowerShell Variable provider supports the Variables that Windows PowerShell creates, including the automatic Variables, the preference Variables, and the Variables that you create.

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

    Most of the Variables are instances of the System.Management.Automation.PSVariable class. However, there are some variations. For example, the “?” Variable is a member of the QuestionMarkVariable class, and the “MaximumVariableCount” Variable is a member of the SessionStateCapacityVariable class.

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

    Windows PowerShell includes a set of cmdlets designed especially to view and to change Variables:

    — Get-Variable
    — New-Variable
    — Set-Variable
    — Remove-Variable
    — Clear-Variable

    When you use these cmdlets, you do not need to specify the Variable: drive in the name.

    The Variable provider supports all of the cmdlets whose names contain the Item noun (the Item cmdlets), except for Invoke-Item. The Variable provider supports the Get-Content and Set-Content cmdlets. However, it does not support the cmdlets whose names contain the ItemProperty noun (the ItemProperty cmdlets), and it does not support the Filter parameter in any cmdlet.

    You can also use the Windows PowerShell expression parser to create, view, and change the values of Variables without using the cmdlets. When working with Variables directly, use a dollar sign ($) to identify the name as a Variable and the assignment operator (=) to establish and change its value. For example, “$p = Get-Process” creates the “p” Variable and stores the results of a “Get-Process” command in it.

    All changes to the 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 console.

CAPABILITIES

TASKS
    TASK: Getting to the Variable: Drive

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

        This command changes the current location to the Variable: drive. 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:”.

        Set-Location Variable:

    TASK: Displaying the Value of Variables

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

        This command gets the list of all the Variables and their values in the current session. You can use this command from any Windows PowerShell drive.

        Get-ChildItem -path Variable:

        ————————– EXAMPLE 2 ————————–

        This command gets the Variables with names that begin with “max”. You can use this command from any Windows PowerShell drive.

        Get-ChildItem -path Variable:max*

        If you are in the Variable: drive, you can omit the drive name from the path.
        ————————– EXAMPLE 3 ————————–

        This command gets the value of the WhatIfPreference Variable by typing it at the command line.

        The name of the Variable is preceded by a dollar sign ($) to indicate that it is a Variable. The Variable: drive name is not specified.

        $WhatIfPreference

        ————————– EXAMPLE 4 ————————–

        This command uses the LiteralPath parameter of Get-ChildItem to get the value of the “?” Variable from within the Variable: drive. Get-ChildItem does not attempt to resolve any wildcards in the values of the LiteralPath parameter.

        Get-ChildItem -literalpath ?

        To display the value of a Variable with a special character name without a cmdlet, type a dollar sign ($) and the Variable name. For example, to display the value of the “?” Variable, type “$?”.
        ————————– EXAMPLE 5 ————————–

        This command gets the Variables that have the values of “ReadOnly” or “Constant” for their Options property.

        Get-ChildItem -path Variable: | Where-Object {$_.options -match “Constant” -or $_.options -match “ReadOnly”} | Format-List -property name, value, options

    TASK: Creating a New Variable

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

        This command creates the “services” Variable and stores the results of a Get-Service command in it. Because the current location is in the Variable: drive, the value of the Path parameter is a dot (.), which represents the current location.

        The parentheses around the Get-Service command ensure that the command is executed before the Variable is created. Without the parentheses, the value of the new Variable is a “Get-Service” string.

        New-Item -path . -name services -value (Get-Service)

        If you are not in the Variable: drive, include the Variable: drive name in the path.
        ————————– EXAMPLE 2 ————————–

        This command creates a “services” Variable and stores the result of a Get-Service command in it.

        The command uses a dollar sign ($) to indicate a Variable and the assignment operator (=) to assign the result of the Get-Service command to the newly created Variable.

        $services = Get-Service

        To create a Variable without a value, omit the assignment operator.

    TASK: Displaying the Properties and Methods of Variables

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

        This command uses the Get-Item cmdlet to get all Variables. The pipeline operator (|) sends the results to the Get-Member cmdlet, which displays the methods and properties of the object.

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

        When you pipe a collection of objects (such as the collection of Variables in the Variable: drive) to Get-Member, Get-Member evaluates each object in the collection separately and returns information about each of the object types that it finds.

        To get information about the collection of objects in the Variable: drive, use the InputObject parameter of Get-Member. For example, “Get-Member -inputobject (Get-Item Variable:*)”. When you use InputObject, Get-Member evaluates the collection, not the objects in the collection.
        ————————– EXAMPLE 2 ————————–

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

        Get-Item Variable:home | Format-List -property *

    TASK: Changing the Properties of a Variable

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

        This command uses the Rename-Item cmdlet to change the name of the “a” Variable to “processes”.

        Rename-Item -path Variable:a -newname processes

        ————————– EXAMPLE 2 ————————–

        This command uses the Set-Item cmdlet to change the value of the ErrorActionPreference Variable to “Stop”.

        Set-Item -path Variable:ErrorActionPreference -value Stop

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

        This command changes the value of the ErrorActionPreference Variable to “Stop”.

        It uses a dollar sign ($) to indicate a Variable and the assignment operator (=) to assign the value.

        $ErrorActionPreference = Stop

    TASK: Copying a Variable

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

        This command uses the Copy-Item cmdlet to copy the “processes” Variable to “old_processes”. This creates a new Variable named “old_processes” that has the same value as the “processes” Variable.

        Copy-Item -path Variable:processes -destination Variable:old_processes

        If the command is issued from within the Variable: drive, you can omit the drive name from the value of the Path parameter.
        ————————– EXAMPLE 2 ————————–

        This command copies the “processes” Variable to “old_processes” without using a cmdlet. It uses the dollar sign ($) to indicate Variables and the assignment operator to assign the value of $processes to old_processes.

        $old_processes = $processes

    TASK: Deleting a Variable

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

        This command deletes the “serv” Variable from the current session. You can use this command in any Windows PowerShell drive.

        Remove-Variable -path Variable:serv

        ————————– EXAMPLE 2 ————————–

        This command deletes all Variables from the current session except for the Variables whose Options property has a value of Constant. Without the Force parameter, the command does not delete Variables whose Options property has a value of ReadOnly.

        Remove-Item Variable:* -force

    TASK: Setting the Value of a Variable to NULL

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

        This command uses the Clear-Item cmdlet to change the value of the “processes” Variable to NULL.

        Clear-Item -path Variable:processes

        ————————– EXAMPLE 2 ————————–

        This command clears the value of the “processes” Variable by assigning a null value to it. It uses the $null automatic Variable to represent the NULL value.

        $processes = $null

DYNAMIC PARAMETERS

NOTES
    The Variable provider does not support any dynamic parameters.

RELATED LINKS
    about_Variables
    about_Automatic_Variables
    about_providers