All posts by Adam

about_Command_Syntax

TOPIC
    about_Command_Syntax

SHORT DESCRIPTION
    Describes the notation used for Windows PowerShell syntax in Help.

LONG DESCRIPTION
    The Get-Help cmdlet displays the parameter usage for a cmdlet. The Get-Help
    cmdlet uses the following special symbols:

        Angle brackets (<>) indicate placeholder text.
        Brackets ([]) indicate optional items.
        Braces ({}) indicate a choice among values.

    Some cmdlets have more than one set of parameters. Distinct parameter sets
    can share some parameters. The Get-Help cmdlet displays all the parameter
    sets for a cmdlet.

    You can find additional information about a parameter in the parameter
    description and in the parameter attribute table. To view complete
    information about a parameter, use the Full or Parameter parameters of
    Get-Help.

Syntax
     Windows PowerShell cmdlet Help, Help topics, and other documentation use
     the following notation for cmdlets in syntax descriptions.

         <cmdlet name> -<Required Parameter name> <Required parameter Value>
                     [-<Optional Parameter name> <Optional Parameter Value>]
                     [-<Optional Switch Parameter>]
                     [-<Optional Parameter Name>] <Required parameter Value>

     The following is the syntax for the New-Alias cmdlet.

         New-Alias [-Force] [-PassThru] [-Scope <string>]
             [-Option {None | ReadOnly | Constant | Private | AllScope}]
             [-Description <string>] [-Name] <string> [-Value] <string>
             [-confirm] [-whatif] [<CommonParameters>]

     The syntax is capitalized for readability, but Windows Powershell is
     case-insensitive.

     Parameters appear in order. The order of parameters is significant only
     when the parameter names are optional. If you do not specify parameter
     names when you use a cmdlet, Windows PowerShell assigns values to
     parameters by position and by type.

     Parameter names are preceded by a hyphen (-). Switch parameters appear
     without a value type. Other parameters appear with the Microsoft .NET
     Framework type of the argument required or with an enumeration of
     possible values. For more information about .NET Framework types,
     see http://go.microsoft.com/fwlink/?LinkId=143682.

     Angle brackets (<>) indicate placeholder text. This text can describe the
     type of an item (such as a string or a Process object). It can also be a
     placeholder for one or more common parameters.

     Brackets ([]) indicate optional items. A parameter can be optional, or
     the name of a required parameter can be optional. Switch parameters are
     always optional.

     Braces ({}) appear around the values of an enumeration. In the example of
     the New-Alias cmdlet, the Option parameter can have any of the listed
     values.

Optional Items
     Brackets ([]) surround optional items. For example, in the New-Alias
     cmdlet syntax description, the Scope parameter is optional. This is
     indicated in the syntax by the brackets around the parameter name
     and type:

         [-Scope <string>]

     Both the following examples are correct uses of the New-Alias cmdlet:

         New-Alias -Name utd -Value Update-TypeData
         New-Alias -Name utd -Value Update-TypeData -Scope global

     A parameter name can be optional even if the value for that parameter is
     required. This is indicated in the syntax by the brackets around the
     parameter name but not the parameter type, as in this example from the
     New-Alias cmdlet:

         [-Name] <string> [-Value] <string>

     The following commands correctly use the New-Alias cmdlet. The commands
     produce the same result:

         New-Alias -Name utd -Value Update-TypeData
         New-Alias -Name utd Update-TypeData
         New-Alias utd -Value Update-TypeData
         New-Alias utd Update-TypeData

     If the parameter name is not included in the statement as typed, Windows
     PowerShell tries to use the position of the arguments to assign the
     values to parameters.

     The following example is not complete:

         New-Alias utd

     This cmdlet requires values for both the Name and Value parameters.

     In syntax examples, brackets are also used in naming and casting to
     .NET Framework types. In this context, brackets do not indicate an
     element is optional.

Array Arguments
     Brackets are also used in syntax descriptions to represent an array.
     The following example shows the syntax for the Restart-Service cmdlet:

         Restart-Service [-Name] <string[]> [-Include <string[]>]
             [-Exclude <string[]>] [-Force] [-PassThru] [-Confirm] [-WhatIf]
             [<CommonParameters>]

     The Name parameter requires an argument. Specifically, it requires the
     name of the service to be restarted. It can take a comma-separated list
     of services, as in the following example:

         Restart-Service RasAuto, RasMan, RemoteAccess

SEE ALSO
    Get-Help

about_command_precedence

TOPIC
    about_command_precedence

SHORT DESCRIPTION
    Describes how Windows PowerShell determines which command to run.

LONG DESCRIPTION
    This topic explains how Windows PowerShell determines which command to
    run, especially when a session contains more than one command with the
    same name. It also explains how to run commands that do not run by
    default, and it explains how to avoid command-name conflicts in your
    session.

COMMAND PRECEDENCE
     When a session includes commands that have the same name, Windows
     PowerShell uses the following rules to decide which command to run.

     These rules become very important when you add commands to your session
     from modules, snap-ins, and other sessions.

     — If you specify the path to a command, Windows PowerShell runs the
         command at the location specified by the path.

         For example, the following command runs the FindDocs.ps1
         script in the C:\TechDocs directory:

             C:\TechDocs\FindDocs.ps1

         As a security feature, Windows PowerShell does not run executable
         (native) commands, including Windows PowerShell scripts, unless the
         command is located in a path that is listed in the Path Environment
         Variable ($env:path) or unless you specify the path to the script
         file.

         To run a script that is in the current directory, specify the full
         path, or type a dot (.) to represent the current directory.

         For example, to run the FindDocs.ps1 file in the current directory,
         type:

             .\FindDocs.ps1

     — If you do not specify a path, Windows PowerShell uses the following
         precedence order when it runs commands:

             1. Alias
             2. Function
             3. Cmdlet
             4. Native Windows commands

         Therefore, if you type “help”, Windows PowerShell first looks for an
         Alias named “help”, then a Function named “Help”, and finally a
         cmdlet named “Help”. It runs the first “help” item that it finds.

         For example, assume you have a Function named Get-Map. Then, you add
         or import a cmdlet named Get-Map. By default, Windows PowerShell
         runs the Function when you type “Get-Map”.

     — When the session contains items of the same type that have the same
         name, such as two cmdlets with the same name, Windows PowerShell
         runs the item that was added to the session most recently.

         For example, assume you have a cmdlet named Get-Date. Then, you import
         another cmdlet named Get-Date. By default, Windows PowerShell runs
         the most-recently imported cmdlet when you type “Get-Date“.

HIDDEN and REPLACED ITEMS
     As a result of these rules, items can be replaced or hidden by items with
     the same name.

     — Items are “hidden” or “shadowed” if you can still access the original
         item, such as by qualifying the item name with a module or snap-in
         name.

         For example, if you import a Function that has the same name as a
         cmdlet in the session, the cmdlet is hidden (but not replaced)
         because it was imported from a snap-in or module.

     — Items are “replaced” or “overwritten” if you can no longer access
         the original item.

         For example, if you import a Variable that has the same name as a
         a Variable in the session, the original Variable is replaced and is
         no longer accessible. You cannot qualify a Variable with a module
         name.

         Also, if you type a Function at the command line and then import
         a Function with the same name, the original Function is replaced and
         is no longer accessible.

RUNNING HIDDEN COMMANDS
     You can run particular commands by specifying item properties that
     distinguish the command from other commands that might have the same
     name.

     You can use this method to run any command, but it is especially useful
     for running hidden commands.

     Use this method as a best practice when writing scripts that you intend
     to distribute because you cannot predict which commands might be present
     in the session in which the script runs.

     QUALIFIED NAMES
     You can run commands that have been imported from a Windows PowerShell
     snap-in or module or from another session by qualifying the command
     name with the name of the module or snap-in in which it originated.

     You can qualify commands, but you cannot qualify Variables or Aliases.

     For example, if the Get-Date cmdlet from the Microsoft.PowerShell.Utility
     snap-in is hidden by an Alias, Function, or cmdlet with the same name, you
     can run it by using the snap-in-qualified name of the cmdlet:

         Microsoft.PowerShell.Utility\Get-Date

     To run a New-Map command that was added by the MapFunctions module, use
     its module-qualified name:

         MapFunctions\New-Map

     To find the snap-in or module from which a command was imported, use the
     following Get-Command command format:

         Get-Command <command-name> | Format-List -property Name, PSSnapin, Module

     For example, to find the source of the Get-Date cmdlet, type:

         Get-Command Get-Date | Format-List -property Name, PSSnapin, Module

         Name     : Get-Date
         PSSnapIn : Microsoft.PowerShell.Utility
         Module :

     CALL OPERATOR
     You can also use the Call operator (&) to run any command that you
     can get by using a Get-ChildItem (the Alias is “dir”), Get-Command, or
     Get-Module command.

     To run a command, enclose the Get-Command command in parentheses,
     and use the Call operator (&) to run the command.

         &(Get-Command …)

     – or –

         &(dir … )

     For example, if you have a Function named Map that is hidden by an
     Alias named Map, use the following command to run the Function.

         &(Get-Command -name map -type Function)

     – or –

         &(dir Function:\map)

     You can also save your hidden command in a Variable to make it
     easier to run.

     For example, the following command saves the Map Function in the
     $myMap Variable and then uses the Call operator to run it.

         $myMap = (Get-Command -name map -type Function)

         &($myMap)

     If a command originated in a module, you can use the following
     format to run it.

         & <PSModuleInfo-object> <command>

     For example, to run the Add-File cmdlet in the FileCommands
     module, use the following command sequence.

         $FileCommands = Get-Module -name FileCommands

         & $FileCommands Add-File

REPLACED ITEMS
     Items that have not been imported from a module or snap-in, such as
     Functions, Variables, and Aliases that you create in your session or
     that you add by using a profile can be replaced by commands that have
     the same name. If they are replaced, you cannot access them.

     Variables and Aliases are always replaced even if they have been
     imported from a module or snap-in because you cannot use a call operator
     or a qualified name to run them.

     For example, if you type a Get-Map Function in your session, and you
     import a Function called Get-Map, the original Function is replaced.
     You cannot retrieve it in the current session.

AVOIDING NAME CONFLICTS
     The best way to manage command name conflicts is to prevent them. When
     you name your commands, use a name that is very specific or is likely to
     be unique. For example, add your initials or company name acronym to the
     nouns in your commands.

     Also, when you import commands into your session from a Windows PowerShell
     module or from another session, use the Prefix parameter of the
     Import-Module or Import-PSSession cmdlet to add a prefix to the nouns
     in the names of commands.

     For example, the following command avoids any conflict with the Get-Date
     and Set-Date cmdlets that come with Windows PowerShell when you import
     the DateFunctions module.

         Import-Module -name DateFunctions -prefix ZZ

     For more information, see Import-Module and Import-PSSession.

SEE ALSO
    about_Path_Syntax
    about_aliases
    about_functions
    Alias (provider)
    Function (provider)
    Get-Command
    Import-Module
    Import-PSSession

about_Break

TOPIC
    about_Break

SHORT DESCRIPTION
    Describes a statement you can use to immediately exit Foreach, For, While,
    Do, or Switch statements.

LONG DESCRIPTION
    When a Break statement appears in a loop, such as a Foreach, For, Switch,
    or While loop, the Break statement causes Windows PowerShell to immediately
    exit the loop. In a Switch construct that does not loop, Break causes
    Windows PowerShell to exit the Switch code block.

    A Break statement can include a label that lets you exit embedded loops.
    A label can specify any loop keyword, such as Foreach, For, or While, in a
    script. When you use a label, Break exits the specified loop. Break exits
    the specified loop, regardless of which loop the Break statement is in.

    The following example shows how to use a Break statement to exit a For
    statement:

        for($i=1; $i -le 10; $i++)
        {
            Write-Host $i
            break
        }

    In this example, the Break statement exits the For loop when the $i
    Variable equals 1. Even though the For statement evaluates to True
    until $i is greater than 10, Windows PowerShell reaches the break statement
    the first time the For loop is run.

    It is more common to use the Break statement in a loop where
    an inner condition must be met. Consider the following Foreach
    statement example:

        $i=0
        $varB = 10,20,30,40
        foreach ($val in $varB)
        {
            $i++
            if ($val -eq 30)
            {
                break
            }
        }
        Write-Host “30 was found in array position $i”

    In this example, the Foreach statement iterates the $varB array. Each
    time the code block is run, the $i Variable is incremented by 1. The
    If statement evaluates to False the first two times the
    loop is run. The third time the loop is run, $i equals 3, and the $val
    Variable equals 30. At this point, the Break statement runs, and the
    Foreach loop exits.

    You break out of the other looping statements in the same way you
    break out of the Foreach loop. In the following example, the Break
    statement exits a While statement when a DivideByZeroException exception
    is trapped using the Trap statement.

        $i = 3
        while ($true)
        {
            trap [DivideByZeroException]
            {
                Write-Host “divide by zero trapped”
                break
            }
            1 / $i–
        }

    A Break statement can include a label. If you use the Break keyword with
    a label, Windows PowerShell exits the labeled loop instead of exiting the
    current loop. The syntax for a label is as follows (this example shows a
    label in a While loop):

        :myLabel while (<condition>) { <statement list>}

    The label is a colon followed by a name that you assign. The label must be
    the first token in a statement, and it must be followed by the looping
    keyword, such as While.

    In Windows PowerShell, only loop keywords, such as Foreach, For, and While
    can have a label.

    Break moves execution out of the labeled loop. In embedded loops, this has
    a different result than the Break keyword has when it is used by itself.
    This schematic example has a While statement with a For statement:

        :myLabel while (<condition 1>)
        {
            for ($item in $items)
            {
                if (<condition 2>) { break myLabel }
                $item = $x # A statement inside the For-loop
            }
        }
        $a = $c # A statement after the labeled While-loop

    If condition 2 evaluates to True, the execution of the script skips down
    to the statement after the labeled loop. In the example, execution starts
    again with the statement “$a = $c”.

    You can nest many labeled loops, as shown in the following schematic
    example.

        :red while (<condition1>)
        {
            :yellow while (<condition2>)
            {
                while (<condition3>)
                {
                    if ($a) {break}
                    if ($b) {break red}
                    if ($c) {break yellow}
                }
                # After innermost loop
            }
                # After “yellow” loop
        }
                # After “red” loop

    If the $b Variable evaluates to True, execution of the script resumes
    after the loop that is labeled “red”. If the $c Variable evaluates to
    True, execution of the script control resumes after the loop that is
    labeled “yellow”.

    If the $a Variable evaluates to True, execution resumes after the innermost
    loop. No label is needed.

    Windows PowerShell does not limit how far labels can resume execution. The
    label can even pass control across script and Function call boundaries.

    The Break keyword is used to leave the Switch construct. For example,
    the following Switch statement uses Break statements to test for the
    most specific condition:

        $var = “word2”
        switch -regex ($var)
        {
            “word2”
            {
                Write-Host “Exact” $_
                break
            }

            “word.*”
            {
                Write-Host “Match on the prefix” $_
                break
            }

            “w.*”
            {
                Write-Host “Match on at least the first letter” $_
                break
            }

            default
            {
                Write-Host “No match” $_
                break
            }
        }

    In this example, the $var Variable is created and initialized to a string
    value of “word2”. The Switch statement uses the Regex class to match the
    Variable value first with the term “word2”. (The Regex class is a regular
    expression Microsoft .NET Framework class.) Because the Variable value and
    the first test in the Switch statement match, the first code block in the
    Switch statement runs.

    When Windows PowerShell reaches the first Break statement, the Switch
    statement exits. If the four Break statements are removed from the example,
    all four conditions are met. This example uses the break statement to
    display results when the most specific condition is met.

SEE ALSO
    about_Comparison_Operators
    about_For
    about_Foreach
    about_Switch
    about_While

about_Automatic_Variables

TOPIC
    about_Automatic_Variables

SHORT DESCRIPTION
    Describes Variables that store state information for Windows PowerShell.
    These Variables are created and maintained by Windows PowerShell.

LONG DESCRIPTION
    Here is a list of the automatic Variables in Windows PowerShell:

    $$
     Contains the last token in the last line received by the session.

    $?
     Contains the execution status of the last operation. It contains
    TRUE if the last operation succeeded and FALSE if it failed.

    $^
     Contains the first token in the last line received by the session.

    $_
     Contains the current object in the pipeline object. You can use this
     Variable in commands that perform an action on every object or on
     selected objects in a pipeline.

    $Args
     Contains an array of the undeclared parameters and/or parameter
     values that are passed to a Function, script, or script block.
     When you create a Function, you can declare the parameters by using the
     param keyword or by adding a comma-separated list of parameters in
     parentheses after the Function name.

    $ConsoleFileName
     Contains the path of the console file (.psc1) that was most
     recently used in the session. This Variable is populated when
     you start Windows PowerShell with the PSConsoleFile parameter or
     when you use the Export-Console cmdlet to export snap-in names to a
     console file.

     When you use the Export-Console cmdlet without parameters, it
     automatically updates the console file that was most recently
     used in the session. You can use this automatic Variable to determine
     which file will be updated.

    $Error
     Contains an array of error objects that represent the most
     recent errors. The most recent error is the first error object in the
     array ($Error[0]).

    $Event
        Contains a PSEventArgs object that represents the event that is being
        processed. This Variable is populated only within the Action block of
        an event registration command, such as Register-ObjectEvent. The value
        of this Variable is the same object that the Get-Event cmdlet returns.
        Therefore, you can use the properties of the $Event Variable, such as
        $Event.TimeGenerated , in an Action script block.

    $EventSubscriber
        Contains a PSEventSubscriber object that represents the event subscriber
        of the event that is being processed. This Variable is populated only
        within the Action block of an event registration command. The value of
        this Variable is the same object that the Get-EventSubscriber cmdlet
        returns.

    $ExecutionContext
     Contains an EngineIntrinsics object that represents the
     execution context of the Windows PowerShell host. You can
     use this Variable to find the execution objects that are
     available to cmdlets.

    $False
     Contains FALSE. You can use this Variable to represent
     FALSE in commands and scripts instead of using the string “false”.
     The string can be interpreted as TRUE if it is converted to a non-empty
     string or to a non-zero integer.

    $ForEach
     Contains the enumerator of a ForEach-Object loop. You can use the
     properties and methods of enumerators on the value of the $ForEach
     Variable. This Variable exists only while the For loop is running. It
     is deleted when the loop is completed.

    $Home
     Contains the full path of the user’s home directory. This Variable is
     the equivalent of the %homedrive%%homepath% Environment Variables,
     typically C:\Documents and Settings\<user>.

    $Host
     Contains an object that represents the current host application
     for Windows PowerShell. You can use this Variable to represent the
     current host in commands or to display or change the properties of
     the host, such as $Host.version or $Host.CurrentCulture, or
     $host.ui.rawui.setbackgroundcolor(“Red”).

    $Input
     An enumerator that contains the input that is passed to a Function. The
     $Input Variable is case-sensitive and is available only in Functions and
     in script blocks. (Script blocks are essentially unnamed Functions.)
     In the Process block of a Function, the $Input Variable contains the
     object that is currently in the pipeline. When the Process block is
     completed, the value of $Input is NULL. If the Function does not have a
     Process block, the value of $Input is available to the End block, and it
     contains all the input to the Function.

    $LastExitCode
     Contains the exit code of the last Windows-based program that was run.

    $Matches
     The $Matches Variable works with the -match and -not match operators.
     When you submit scalar input to the -match or -notmatch operator, and
     either one detects a match, they return a Boolean value and populate
     the $Matches automatic Variable with a hash table of any string values
     that were matched. For more information about the -match operator, see
     about_Comparison_Operators.

    $MyInvocation
     Contains an object with information about the current command, such as
     a script, Function, or script block. You can use the information in the
     object, such as the path and file name of the script
     ($myinvocation.mycommand.path) or the name of a Function
     ($myinvocation.mycommand.name) to identify the current command. This is
     particularly useful for finding the name of the script that is running.

    $NestedPromptLevel
     Contains the current prompt level. A value of 0 indicates the original
     prompt level. The value is incremented when you enter a nested level and
     decremented when you exit it.

     For example, Windows PowerShell presents a nested command prompt when
     you use the $Host.EnterNestedPrompt method. Windows PowerShell also
     presents a nested command prompt when you reach a breakpoint in the
     Windows PowerShell debugger.

     When you enter a nested prompt, Windows PowerShell pauses the current
     command, saves the execution context, and increments the value of
     the $NestedPromptLevel Variable. To create additional nested command
     prompts (up to 128 levels) or to return to the original command prompt,
     complete the command, or type “exit”.

     The $NestedPromptLevel Variable helps you track the prompt level. You
     can create an alternative Windows PowerShell command prompt that
     includes this value so that it is always visible.

    $NULL
     Contains a NULL or empty value. You can use this Variable to
     represent NULL in commands and scripts instead of using the string
     “NULL”. The string can be interpreted as TRUE if it is converted to a
     non-empty string or a non-zero integer.

    $PID
     Contains the process identifier (PID) of the process that is hosting
     the current Windows PowerShell session.

    $Profile
     Contains the full path of the Windows PowerShell profile for the
     current user and the current host application. You can use this Variable
     to represent the profile in commands. For example, you can use it in
     a command to determine whether a profile has been created:

         Test-Path $profile

     Or, you can use it in a command to create a profile:

         New-Item -type file -path $pshome -force

     You can also use it in a command to open the profile in Notepad:

         notepad $profile

    $PSBoundParameters
     Contains a dictionary of the active parameters and their current
     values. This Variable has a value only in a scope where parameters
     are declared, such as a script or Function. You can use it to
     display or change the current values of parameters or to pass
     parameter values to another script or Function.

     For example:

         Function test {
            param($a, $b)

            # Display the parameters in dictionary format.
            $psboundparameters

            # Call the Test1 Function with $a and $b.
            test1 @psboundparameters
         }

    $PsCmdlet
     Contains an object that represents the cmdlet or advanced Function
     that is being run.

     You can use the properties and methods of the object in your cmdlet
     or Function code to respond to the conditions of use. For example,
     the ParameterSetName property contains the name of the parameter set
     that is being used, and the ShouldProcess method adds the WhatIf and
     Confirm parameters to the cmdlet dynamically.

     For more information about the $PSCmdlet automatic Variable, see
     about_functions_advanced.

    $PsCulture
     Contains the name of the culture currently in use in the operating
     system. The culture determines the display format of items such
     as numbers, currrency, and dates. This is the value of the
     System.Globalization.CultureInfo.CurrentCulture.Name property of the
     system. To get the System.Globalization.CultureInfo object for the
     system, use the Get-Culture cmdlet.

    $PSDebugContext
     While debugging, this Variable contains information about the
     debugging Environment. Otherwise, it contains a NULL value.
     As a result, you can use it to indicate whether the debugger has
     control. When populated, it contains a PsDebugContext object that has
     Breakpoints and InvocationInfo properties. The InvocationInfo property
     has several useful properties, including the Location property. The
     Location property indicates the path of the script that is being
     debugged.

    $PsHome
     Contains the full path of the installation directory for Windows
     PowerShell, typically, %windir%\System32\WindowsPowerShell\v1.0. You
     can use this Variable in the paths of Windows PowerShell files. For
     example, the following command searches the conceptual Help topics for
     the word “variable”:

            Select-String -pattern Variable -path $pshome\*.txt

    $PSScriptRoot
     Contains the directory from which the script module is being executed.
     This Variable allows scripts to use the module path to access other
     resources.

    $PsUICulture
     Contains the name of the user interface (UI) culture that is currently
     in use in the operating system. The UI culture determines which text
     strings are used for user interface elements, such as menus and
     messages. This is the value of the
     System.Globalization.CultureInfo.CurrentUICulture.Name property of the
     system. To get the System.Globalization.CultureInfo object for the
     system, use the Get-UICulture cmdlet.

    $PsVersionTable
        Contains a read-only hash table that displays details about the
        version of Windows PowerShell that is running in the current session.
        The table includes the following items:

        CLRVersion:            The version of the common language runtime (CLR)

        BuildVersion:         The build number of the current version

            PSVersion:             The Windows PowerShell version number

            WSManStackVersion:     The version number of the WS-Management stack

        PSCompatibleVersions: Versions of Windows PowerShell that are
                                 compatible with the current version

            SerializationVersion The version of the serialization method

            PSRemotingProtocolVersion
                                 The version of the Windows PowerShell remote
                                 management protocol

    $Pwd
     Contains a path object that represents the full path of the current
     directory.

    $Sender
        Contains the object that generated this event. This Variable is
        populated only within the Action block of an event registration command.
        The value of this Variable can also be found in the Sender property of
        the PSEventArgs (System.Management.Automation.PSEventArgs) object that
        Get-Event returns.

    $ShellID
     Contains the identifier of the current shell.

    $SourceArgs
        Contains objects that represent the event arguments of the event that
        is being processed. This Variable is populated only within the Action
        block of an event registration command. The value of this Variable
        can also be found in the SourceArgs property of the PSEventArgs
        (System.Management.Automation.PSEventArgs) object that Get-Event
        returns.

    $SourceEventArgs
        Contains an object that represents the first event argument that derives
        from EventArgs of the event that is being processed. This Variable is
        populated only within the Action block of an event registration command.
        The value of this Variable can also be found in the SourceArgs property
        of the PSEventArgs (System.Management.Automation.PSEventArgs) object
        that Get-Event returns.

    $This
        In a script block that defines a script property or script method, the
        $This Variable refers to the object that is being extended.

    $True
     Contains TRUE. You can use this Variable to represent
     TRUE in commands and scripts.

SEE ALSO
    about_hash_tables
    about_preference_variables
    about_Variables

about_Assignment_Operators

TOPIC
    about_Assignment_Operators

SHORT DESCRIPTION
    Describes how to use operators to assign values to Variables.

LONG DESCRIPTION
    Assignment operators assign one or more values to a Variable. They can
    perform numeric operations on the values before the assignment.

    Windows PowerShell supports the following assignment operators.

    Operator Description
    ——- ———–
    =         Sets the value of a Variable to the specified value.

    +=        Increases the value of a Variable by the specified value, or
             appends the specified value to the existing value.

    -=        Decreases the value of a Variable by the specified value.

    *=        Multiplies the value of a Variable by the specified value, or
             appends the specified value to the existing value.

    /=        Divides the value of a Variable by the specified value.

    %=        Divides the value of a Variable by the specified value and
             then assigns the remainder (modulus) to the Variable.

    ++        Increases the value of a Variable, assignable property, or
             array element by 1.

    —        Decreases the value of a Variable, assignable property, or
             array element by 1.

SYNTAX
    The syntax of the assignment operators is as follows:

        <assignable-expression> <assignment-operator> <value>

    Assignable expressions include Variables and properties. The value can be
    a single value, an array of values, or a command, expression, or statement.

    The increment and decrement operators are unary operators. Each has prefix
    and postfix versions.

        <assignable-expression><operator>
        <operator><assignable-expression>

    The assignable expression must a number or it must be convertible to a
    number.

ASSIGNING VALUES
    Variables are named memory spaces that store values. You store the values
    in Variables by using the assignment operator (=). The new value can
    replace the existing value of the Variable, or you can append a new value
    to the existing value.

    The basic assignment operator is the equal sign (=)(ASCII 61). For example,
    the following statement assigns the value Windows PowerShell to the
    $MyShell Variable:

        $MyShell = “Windows PowerShell”

    When you assign a value to a Variable in Windows PowerShell, the Variable
    is created if it did not already exist. For example, the first of the
    following two assignement statements creates the $a Variable and assigns
    a value of 6 to $a. The second assignment statement assigns a value
    of 12 to $a. The first statement creates a new Variable. The second
    statement changes only its value:

        $a = 6
        $a = 12

    Variables in Windows PowerShell do not have a specific data type unless you
    cast them. When a Variable contains only one object, the Variable takes the
    data type of that object. When a Variable contains a collection of objects,
    the Variable has the System.Object data type. Therefore, you can assign any
    type of object to the collection. The following example shows that you can
    add process objects, service objects, strings, and integers to a Variable
    without generating an error:

        $a = Get-Process
        $a += Get-Service
        $a += “string”
        $a += 12

    Because the assignment operator (=) has a lower precedence than the
    pipeline operator (|), parentheses are not required to assign the result
    of a command pipeline to a Variable. For example, the following command
    sorts the services on the computer and then assigns the sorted services
    to the $a Variable:

        $a = Get-Service | sort name

    You can also assign the value created by a statement to a Variable, as in
    the following example:

        $a = if ($b -lt 0) { 0 } else { $b }

    This example assigns 0 to the $a Variable if the value of $b is less
    than 0. It assigns the value of $b to $a if the value of $b is not less
    than zero.

THE ASSIGNMENT OPERATOR (=)
    The assignment operator (=) assigns values to Variables. If the Variable
    already has a value, the assignment operator (=) replaces the value
    without warning.

    The following statement assigns the integer value 6 to the $a Variable:

        $a = 6

    To assign a string value to a Variable, enclose the string value in
    quotation marks, as follows:

        $a = “baseball”

    To assign an array (multiple values) to a Variable, separate the values
    with commas, as follows:

        $a = “apple”, “orange”, “lemon”, “grape”

    To assign a hash table to a Variable, use the standard hash table notation
    in Windows PowerShell. Type an at sign (@) followed by key/value pairs that
    are separated by semicolons (;) and enclosed in braces ({ }). For example,
    to assign a hash table to the $a Variable, type:

        $a = @{one=1; two=2; three=3}

    To assign hexadecimal values to a Variable, precede the value with “0x”.
    Windows PowerShell converts the hexadecimal value (0x10) to a decimal
    value (in this case, 16) and assigns that value to the $a Variable. For
    example, to assign a value of 0x10 to the $a Variable, type:

        $a = 0x10

    To assign an exponential value to a Variable, type the root number, the
    letter “e”, and a number that represents a multiple of 10. For example, to
    assign a value of 3.1415 to the power of 1,000 to the $a Variable, type:

        $a = 3.1415e3

    Windows PowerShell can also convert kilobytes (KB), megabytes (MB), and
    gigabytes (GB) into bytes. For example, to assign a value of 10 kilobytes
    to the $a Variable, type:

        $a = 10kb

THE ASSIGNMENT BY ADDITION OPERATOR (+=)
    The assignment by addition operator (+=) either increments the value of a
    Variable or appends the specified value to the existing value. The action
    depends on whether the Variable has a numeric or string type and whether
    the Variable contains a single value (a scalar) or multiple values
    (a collection).

    The += operator combines two operations. First, it adds, and then it
    assigns. Therefore, the following statements are equivalent:

        $a += 2
        $a = ($a + 2)

    When the Variable contains a single numeric value, the += operator
    increments the existing value by the amount on the right side of the
    operator. Then, the operator assigns the resulting value to the Variable.
    The following example shows how to use the += operator to increase the
    value of a Variable:

        C:\PS> $a = 4
        C:\PS> $a += 2
        C:\PS> $a
        6

    When the value of the Variable is a string, the value on the right side of
    the operator is appended to the string, as follows:

        C:\PS> $a = “Windows”
        C:\PS> $a +- ” PowerShell”
        C:\PS> $a
        Windows PowerShell

    When the value of the Variable is an array, the += operator appends the
    values on the right side of the operator to the array. Unless the array is
    explicitly typed by casting, you can append any type of value to the array,
    as follows:

        C:\PS> $a = 1,2,3
        C:\PS> $a += 2
        C:\PS> $a
        1
        2
        3
        2
        C:\PS> $a += “String”
        C:\PS> $a
        1
        2
        3
        2
        String

    When the value of a Variable is a hash table, the += operator appends the
    value on the right side of the operator to the hash table. However, because
    the only type that you can add to a hash table is another hash table, all
    other assignments fail.

    For example, the following command assigns a hash table to the $a Variable.
    Then, it uses the += operator to append another hash table to the existing
    hash table, effectively adding a new key/value pair to the existing hash
    table. This command succeeds, as shown in the output:

        C:\PS> $a = @{a = 1; b = 2; c = 3}
        C:\PS> $a += @{mode = “write”}
        C:\PS> $a
        Name                         Value
        —-                         —–
        a                             1
        b                             2
        mode                         write
        c                             3

    The following command attempts to append an integer (1) to the hash table
    in the $a Variable. This command fails:

        C:\PS> $a = @{a = 1; b = 2; c = 3}
        C:\PS> $a += 1
        You can add another hash table only to a hash table.
        At line:1 char:6
        + $a += <<<< 1

THE ASSIGNMENT BY SUBTRACTION OPERATOR (-=)
    The assignment by subtraction operator (-=) decrements the value of a
    Variable by the value that is specified on the right side of the operator.
    This operator cannot be used with string Variables, and it cannot be used
    to remove an element from a collection.

    The -= operator combines two operations. First, it subtracts, and then it
    assigns. Therefore, the following statements are equivalent:

        $a -= 2
        $a = ($a – 2)

    The following example shows how to use of the -= operator to decrease the
    value of a Variable:

        C:\PS> $a = 8
        C:\PS> $a -= 2
        C:\PS> $a
        6

    You can also use the -= assignment operator to decrease the value of a
    member of a numeric array. To do this, specify the index of the array
    element that you want to change. In the following example, the value of the
    third element of an array (element 2) is decreased by 1:

        C:\PS> $a = 1,2,3
        C:\PS> $a[2] -= 1.
        C:\PS> $a
        1
        2
        2

    You cannot use the -= operator to delete the values of a Variable. To
    delete all the values that are assigned to a Variable, use the Clear-Item
    or Clear-Variable cmdlets to assign a value of $null or “” to the Variable.

        $a = $null

    To delete a particular value from an array, use array notation to assign a
    value of $null to the particular item. For example, the following statement
    deletes the second value (index position 1) from an array:

        C:\PS> $a = 1,2,3
        C:\PS> $a
        1
        2
        3

        C:\PS> $a[1] = $null
        C:\PS> $a
        1
        3

    To delete a Variable, use the Remove-Variable cmdlet. This method is useful
    when the Variable is explicitly cast to a particular data type, and you
    want an untyped Variable. The following command deletes the $a Variable:

        Remove-Variable a

THE ASSIGNMENT BY MULTIPLICATION OPERATOR (*=)
    The assignment by multiplication operator (*=) multiplies a numeric value
    or appends the specified number of copies of the string value of a
    Variable.

    When a Variable contains a single numeric value, that value is multiplied
    by the value on the right side of the operator. For example, the following
    example shows how to use the *= operator to multiply the value of a
    Variable:

        C:\PS> $a = 3
        C:\PS> $a *= 4
        C:\PS> $a
        12

    In this case, the *= operator combines two operations. First, it
    multiplies, and then it assigns. Therefore, the following statements are
    equivalent:

        $a *= 2
        $a = ($a * 2)

    When a Variable contains a string value, Windows PowerShell appends the
    specified number of strings to the value, as follows:

        C:\PS> $a = “file”
        C:\PS> $a *= 4
        C:\PS> $a
        filefilefilefile

    To multiply an element of an array, use an index to identify the element
    that you want to multiply. For example, the following command multiplies
    the first element in the array (index position 0) by 2:

        $a[0] *= 2

THE ASSIGNMENT BY DIVISION OPERATOR (/=)
    The assignment by division operator (/=) divides a numeric value by the
    value that is specified on the right side of the operator. The operator
    cannot be used with string Variables.

    The /= operator combines two operations. First, it divides, and then it
    assigns. Therefore, the following two statements are equivalent:

        $a /= 2
        $a = ($a / 2)

    For example, the following command uses the /= operator to divide the value
    of a Variable:

        C:\PS> $a = 8
        C:\PS> $a /=2
        C:\PS> $a
        4

    To divide an element of an array, use an index to identify the element that
    you want to change. For example, the following command divides the second
    element in the array (index position 1) by 2:

        $a[1] /= 2

THE ASSIGNMENT BY MODULUS OPERATOR (%=)
    The assignment by modulus operator (%=) divides the value of a Variable by
    the value on the right side of the operator. Then, the %= operator assigns
    the remainder (known as the modulus) to the Variable. You can use this
    operator only when a Variable contains a single numeric value. You cannot
    use this operator when a Variable contains a string Variable or an array.

    The %= operator combines two operations. First, it divides and determines
    the remainder, and then it assigns the remainder to the Variable.
    Therefore, the following statements are equivalent:

        $a %= 2
        $a = ($a % 2)

    The following example shows how to use the %= operator to save the modulus
    of a quotient:

        C:\PS> $a = 7
        C:\PS> $a %= 4
        C:\PS> $a
        3

THE INCREMENT AND DECREMENT OPERATORS

    The increment operator (++) increases the value of a Variable by 1. When
    you use the increment operator in a simple statement, no value is returned.
    To view the result, display the value of the Variable, as follows:

        C:\PS> $a = 7
        C:\PS> ++$a
        C:\PS> $a
        8

    To force a value to be returned, enclose the Variable and the operator in
    parentheses, as follows:

        C:\PS> $a = 7
        C:\PS> (++$a)
        8

    The increment operator can be placed before (prefix) or after (postfix) a
    Variable. The prefix version of the operator increments a Variable before
    its value is used in the statement, as follows:

        C:\PS> $a = 7
        C:\PS> $c = ++$a
        C:\PS> $a
        8
        C:\PS> $c
        8

    The postfix version of the operator increments a Variable after its value
    is used in the statement. In the following example, the $c and $a Variables
    have different values because the value is assigned to $c before $a
    changes:

        C:\PS> $a = 7
        C:\PS> $c = $a++
        C:\PS> $a
        8
        C:\PS> $c
        7

    The decrement operator (–) decreases the value of a Variable by 1. As with
    the increment operator, no value is returned when you use the operator in a
    simple statement. Use parentheses to return a value, as follows:

        C:\PS> $a = 7
        C:\PS> –$a
        C:\PS> $a
        6
        C:\PS> (–$a)
        5

    The prefix version of the operator decrements a Variable before its value
    is used in the statement, as follows:

        C:\PS> $a = 7
        C:\PS> $c = –$a
        C:\PS> $a
        6
        C:\PS> $c
        6

    The postfix version of the operator decrements a Variable after its value
    is used in the statement. In the following example, the $d and $a Variables
    have different values because the value is assigned to $d before $a
    changes:

        C:\PS> $a = 7
        C:\PS> $d = $a–
        C:\PS> $a
        6
        C:\PS> $d
        7

MICROSOFT .NET FRAMEWORK TYPES
    By default, when a Variable has only one value, the value that is assigned
    to the Variable determines the data type of the Variable. For example, the
    following command creates a Variable that has the Integer (System.Int32)
    type:

        $a = 6

    To find the .NET Framework type of a Variable, use the GetType method and
    its FullName property, as follows. Be sure to include the parentheses after
    the GetType method name, even though the method call has no arguments:

        C:\PS> $a = 6
        C:\PS> $a.gettype().fullname
        System.Int32

    To create a Variable that contains a string, assign a string value to the
    Variable. To indicate that the value is a string, enclose it in quotation
    marks, as follows:

        C:\PS> $a = “6”
        C:\PS> $a.gettype().fullname
        System.String

    If the first value that is assigned to the Variable is a string, Windows
    PowerShell treats all operations as string operations and casts new values
    to strings. This occurs in the following example:

        C:\PS> $a = “file”
        C:\PS> $a += 3
        C:\PS> $a
        file3

    If the first value is an integer, Windows PowerShell treats all operations
    as integer operations and casts new values to integers. This occurs in the
    following example:

        C:\PS> $a = 6
        C:\PS> $a += “3”
        C:\PS> $a
        9

    You can cast a new scalar Variable as any .NET Framework type by placing
    the type name in brackets that precede either the Variable name or
    the first assignment value. When you cast a Variable, you can determine the
    types of data that can be stored in the Variable. And, you can determine
    how the Variable behaves when you manipulate it.

    For example, the following command casts the Variable as a string type:

        C:\PS> [string]$a = 27
        C:\PS> $a += 3
        C:\PS> $a
        273

    The following example casts the first value, instead of casting the
    Variable:

        $a = [string]27

    When you cast a Variable to a specific type, the common convention is to
    cast the Variable, not the value. However, you cannot recast the data type
    of an existing Variable if its value cannot be converted to the new data
    type. To change the data type, you must replace its value, as follows:

        C:\PS> $a = “string”
        C:\PS> [int]$a
        Cannot convert value “string” to type “System.Int32”. Error: “Input
        string was not in a correct format.”
        At line:1 char:8
        + [int]$a <<<<

        C:\PS> [int]$a =3

    In addition, when you precede a Variable name with a data type, the type
    of that Variable is locked unless you explicitly override the type by
    specifying another data type. If you try to assign a value that is
    incompatible with the existing type, and you do not explicitly override the
    type, Windows PowerShell displays an error, as shown in the following
    example:

        C:\PS> $a = 3
        C:\PS> $a = “string”

        C:\PS> [int]$a = 3
        C:\PS> $a = “string”
        Cannot convert value “string” to type “System.Int32”. Error: “Input
        string was not in a correct format.”
        At line:1 char:3
        + $a <<<< = “string”

        C:\PS> [string]$a = “string”

    In Windows PowerShell, the data types of Variables that contain multiple
    items in an array are handled differently from the data types of Variables
    that contain a single item. Unless a data type is specifically assigned to
    an array Variable, the data type is always System.Object []. This data type
    is specific to arrays.

    Sometimes, you can override the default type by specifying another
    type. For example, the following command casts the Variable as a string []
    array type:

        [string []] $a = “one”, “two”, “three”

    Windows PowerShell Variables can be any .NET Framework data type. In
    addition, you can assign any fully qualified .NET Framework data type that
    is available in the current process. For example, the following command
    specifies a System.DateTime data type:

        [system.datetime]$a = “5/31/2005”

    The Variable will be assigned a value that conforms to the System.DateTime
    data type. The value of the $a Variable would be the following:

        Tuesday, May 31, 2005 12:00:00 AM

ASSIGNING MULTIPLE VariableS
    In Windows PowerShell, you can assign values to multiple Variables by using
    a single command. The first element of the assignment value is assigned to
    the first Variable, the second element is assigned to the second Variable,
    the third element to the third Variable, and so on. For example, the
    following command assigns the value 1 to the $a Variable, the value 2 to
    the $b Variable, and the value 3 to the $c Variable:

        C:\PS> $a, $b, $c = 1, 2, 3

    If the assignment value contains more elements than Variables, all the
    remaining values are assigned to the last Variable. For example, the
    following command contains three Variables and five values:

        $a, $b, $c = 1, 2, 3, 4, 5

    Therefore, Windows PowerShell assigns the value 1 to the $a Variable and
    the value 2 to the $b Variable. It assigns the values 3, 4, and 5 to
    the $c Variable. To assign the values in the $c Variable to three other
    Variables, use the following format:

        $d, $e, $f = $c

    This command assigns the value 3 to the $d Variable, the value 4 to
    the $e Variable, and the value 5 to the $f Variable.

    You can also assign a single value to multiple Variables by chaining the
    Variables. For example, the following command assigns a value of “three”
    to all four Variables:

        $a = $b = $c = $d = “three”

Variable-RELATED CMDLETS
    In addition to using an assignment operation to set a Variable value, you
    can also use the Set-Variable cmdlet. For example, the following command
    uses Set-Variable to assign an array of 1, 2, 3 to the $a Variable.

        Set-Variable -name a -value 1, 2, 3

SEE ALSO
    about_arrays
    about_hash_tables
    about_Variables
    Clear-Variable
    Remove-Variable
    Set-Variable

about_arrays

TOPIC
    about_arrays

SHORT DESCRIPTION
    Describes a compact data structure for storing data elements.

LONG DESCRIPTION
    An array is a data structure for storing a collection of data elements
    of the same type. Windows PowerShell supports data elements, such as
    string, int (32-bit integer), long (64-bit integer), bool (Boolean), byte,
    and other Microsoft .NET Framework object types.

CREATING AND INITIALIZING AN ARRAY
    To create and initialize an array, assign multiple values to a Variable.
    The values stored in the array are delimited with a comma and separated
    from the Variable name by the assignment operator (=).

    For example, to create an array named $A that contains the seven
    numeric (int) values of 22, 5, 10, 8, 12, 9, and 80, type:

        $A = 22,5,10,8,12,9,80

    You can also create and initialize an array by using the range
    operator (..). For example, to create and initialize an array named
    “$B” that contains the values 5 through 8, type:

        $B = 5..8

    As a result, $B contains four values: 5, 6, 7, and 8.

    When no data type is specified, Windows PowerShell creates each array as
    an object array (type: object[]). To determine the data type of an array,
    use the GetType() method. For example, to determine the data type of the
    $a array, type:

        $a.gettype()

    To create a strongly typed array, that is, an array that can contain only
    values of a particular type, cast the Variable as an array type, such
    as string[], long[], or int32[]. To cast an array, precede the Variable
    name with an array type enclosed in brackets. For example, to create a
    32-bit integer array named $ia containing four integers (1500, 2230, 3350,
    and 4000), type:

        [int32[]]$ia = 1500,2230,3350,4000

    As a result, the $ia array can contain only integers.

    You can create arrays that are cast to any supported type in the
    Microsoft .NET Framework. For example, the objects that Get-Process
    retrieves to represent processes are of the System.Diagnostics.Process
    type. To create a strongly typed array of process objects, enter the
    following command:

        [Diagnostics.Process[]]$zz = Get-Process

    You can populate an array by using the output of a cmdlet, Function, or
    statement. For example, the following statement creates an array that
    contains the processes that start with the letters “co” and that are
    running on the current computer:

        $LocalProcesses = Get-Process co*

    If the statement gets only a single process, the $LocalProcesses Variable
    would not be an array. To ensure the command creates an array, use the
    array subexpression operator, @, as shown in the following example:

        $LocalProcesses = @(Get-Process co*)

    Even if the command returns a single process, the $LocalProcesses Variable
    is an array. Even if it has only one member, you can treat it like any
    other array. For example, you can add other objects to it. For more
    information, see about_operators.

READING AN ARRAY
    You can refer to an array by using its Variable name, such as $A or $a.
    Windows PowerShell is not case-sensitive.

    To display all the elements in the array, type the array name. For example:

    $a

    You can refer to the elements in an array by using an index, beginning
    at position 0. Enclose the index number in brackets. For example,
    to display the first element in the $a array, type:

        $a[0]

    To display the third element in the $a array, type:

        $a[2]

    Negative numbers count from the end of the array. For example, “-1”
    refers to the last element of the array. To display the last three elements
    of the array, type:

        $a[-3..-1]

    However, be cautious when using this notation.

        $a[0..-2]

    This command does not refer to all the elements of the array, except for
    the last one. It refers to the first, last, and second-to-last elements
    in the array.

    You can use the range operator to display a subset of all the values in an
    array. For example, to display the data elements at index position 1
    through 3, type:

        $a[1..3]

    You can use the plus operator (+) to combine a range with a list of
    elements in an array. For example, to display the elements at index
    positions 0, 2, and 4 through 6, type:

        $a[0,2+4..6]

    To determine how many items are in an array, combine the range with the
    length property of an array. For example, to display the elements from
    index position 2 to the end of the array, type:

        $a[2..($a.length-1)]

    The length is set to -1 because the index begins at position 0. Therefore,
    in a three-element array (1,2,3), the index of the third element is 2,
    which is one less than the length of the array.

    You can also use looping constructs, such as Foreach, For, and While loops,
    to refer to the elements in an array. For example, to use a Foreach loop
    to display the elements in the $a array, type:

        foreach ($element in $a) {$element}

    The Foreach loop iterates through the array and returns each value in
    the array until reaching the end of the array.

    The For loop is useful when you are incrementing counters while examining
    the elements in an array. For example, to return every other value in an
    array by using a For loop, type:

        for ($i = 0; $i -le ($a.length – 1); $i += 2) {$a[$i]}

    You can use a While loop to display the elements in an array until a
    defined condition is no longer true. For example, to display the elements
    in the $a array while the array index is less than 4, type:

        $i=0
        while($i -lt 4) {$a[$i]; $i++}

    To learn about the properties and methods of an array, such as the Length
    property and the SetValue method, use the InputObject parameter of the
    Get-Member cmdlet. When you pipe an array to Get-Member, it displays
    information about the objects in the array. When you use the InputObject
    parameter, it displays information about the array.

    To find the properties and methods of the $a array, type:

    Get-Member -inputobject $a

MANIPULATING AN ARRAY
    You can change the elements in an array, add an element to an array, and
    combine the values from two arrays into a third array.

    To change the value of a particular element in an array, specify the
    array name and the index of the element that you want to change, and then
    use the assignment operator (=) to specify a new value for the element. For
    example, to change the value of the second item in the $a array (index
    position 1) to 10, type:

        $a[1] = 10

    You can also use the SetValue method of an array to change a value. The
    following example changes the second value (index position 1) of the $a
    array to 500:

        $a.SetValue(500,1)

    You can append an element to an existing array by using the += operator.
    This operator adds to an existing value. When the operator is used on an
    element of an array, it increases the value of the element. When the
    operator is used on the array itself, it appends the value. For example,
    to append an element with a value of 200 to the $a array, type:

        $a += 200

    It is not easy to delete elements from an array, but you can create a new
    array that contains only selected elements of an existing array. For
    example, to create the $t array with all the elements in the $a array
    except for the value at index position 2, type:

        $t = $a[0,1 + 3..($a.length – 1)]

    To combine two arrays into a single array, use the plus operator (+). The
    following example creates two arrays, combines them, and then displays
    the resulting combined array.

        $x = 1,3
        $y = 5,9
        $z = $x + $y

    As a result, the $z array contains 1, 3, 5, and 9.

    To delete an array, use the Remove-Item cmdlet to delete the Variable that
    contains the array. The following command specifies the element “a” in the
    Variable: drive.

        Remove-Item Variable:a

    (For more information about the Variable: drive, see the Variable provider
    Help topic.)

SEE ALSO
    about_Assignment_Operators
    about_hash_tables
    about_operators
    about_For
    about_Foreach
    about_While

about_Arithmetic_Operators

TOPIC
    about_Arithmetic_Operators

SHORT DESCRIPTION
    Describes the operators that perform arithmetic in Windows PowerShell.

LONG DESCRIPTION

    Arithmetic operators calculate numeric values. You can use one or
    more arithmetic operators to add, subtract, multiply, and divide
    values, and to calculate the remainder (modulus) of a division operation.

    In addition, the addition operator (+) and multiplication operator (*)
    also operate on strings, arrays, and hash tables. The addition operator
    concatenates the input. The multiplication operator returns multiple copies
    of the input. You can even mix object types in an arithmetic statement.
    The method that is used to evaluate the statement is determined by the type
    of the leftmost object in the expression.

    Windows PowerShell supports the following arithmetic operators:

    Operator Description                             Example
    ——– ———–                             ——-
    +         Adds integers; concatenates strings,    6+2
             arrays, and hash tables.                “file” + “name”

    –         Subtracts one value from another        6-2
             value.                                 (Get-Date).date – 1

    –         Makes a number a negative number.     -6+2
                                                     -4

    *         Multiplies integers; copies strings     6*2
             and arrays the specified number of     “w” * 3
             times.

    /         Divides two values.                     6/2

    %         Returns the remainder of a division     7%2
             operation.

    OPERATOR PRECEDENCE
    Windows PowerShell processes arithmetic operators in the following order:

        Parentheses ()
        – (for a negative number)
        *, /, %
        +, – (for subtraction)

    Windows PowerShell processes the expressions from left to right according
    to the precedence rules. The following examples show the effect of the
    precedence rules:

        C:\PS> 3+6/3*4
        11

        C:\PS> 10+4/2
        12

        C:\PS> (10+4)/2
        7

        C:\PS> (3+3)/ (1+1)
        3

    The order in which Windows PowerShell evaluates expressions might differ
    from other programming and scripting languages that you have used. The
    following example shows a complicated assignment statement.

        C:\PS> $a = 0
        C:\PS> $b = 1,2
        C:\PS> $c = -1,-2

        C:\PS> $b[$a] = $c[$a++]

        C:\PS> $b
        1
        -1

    In this example, the expression $a++ is evaluated before $c[$a++].
    Evaluating $a++ changes the value of $a. The Variable $a in $b[$a]
    equals 1, not 0, so the statement assigns a value to $b[1], not $b[0].

    ADDING AND MULTIPLYING NON-NUMERIC TYPES
    You can add numbers, strings, arrays, and hash tables. And, you can
    multiply numbers, strings, and arrays. However, you cannot multiply hash
    tables.

    When you add strings, arrays, or hash tables, the elements are
    concatenated. When you concatenate collections, such as arrays or hash
    tables, a new object is created that contains the objects from both
    collections. If you try to concatenate hash tables that have the same key,
    the operation fails.

    For example, the following commands create two arrays and then add them:

    C:\PS> $a = 1,2,3
    C:\PS> $b = “A”,”B,”C”
    C:\PS> $a + $b
    1
    2
    3
    A
    B
    C

    You can also perform arithmetic operations on objects of different types.
    The operation that Windows PowerShell performs is determined by the
    Microsoft .NET Framework type of the leftmost object in the operation.
    Windows PowerShell tries to convert all the objects in the operation to the
    .NET Framework type of the first
    object. If it succeeds in converting the objects, it performs the operation
    appropriate to the .NET Framework type of the first object. If it fails to
    convert any of the objects, the operation fails.

    The following example demonstrates the use of the addition and
    multiplication operators in operations that include different object types:

        C:\PS> “file” + 16
        file16

        C:\PS> $array = 1,2,3
        C:\PS> $array + 16
        1
        2
        3
        16

    C:\PS> $array + “file”
        1
        2
        3
        file

        C:\PS> “file” * 3
        filefilefile

    Because the method that is used to evaluate statements is determined by the
    leftmost object, addition and multiplication in Windows PowerShell are not
    strictly commutative. For example, (a + b) does not always equal (b + a),
    and (a * b) does not always equal (b * a).

    The following examples demonstrate this principle:

        C:\PS> “file” + 2
        file2

        C:\PS> 2 + “file”
        Cannot convert value “file” to type “System.Int32”. Error: “Input
        string was not in a correct format.”
        At line:1 char:4
        + 2 + <<<< “file”

        C:\PS> “file” * 3
        filefilefile

        C:\PS> 3 * “file”
        Cannot convert value “file” to type “System.Int32”. Error: “Input
        string was not in a correct format.”
        At line:1 char:4
        + 3 * <<<< “file”

    Hash tables are a slightly different case. You can add hash tables. And,
    you can add a hash table to an array. However, you cannot add any other
    type to a hash table.

    The following examples show how to add hash tables to each other and to
    other objects:

        C:\PS> $hash1 = @{a=1; b=2; c=3}
        C:\PS> $hash2 = @{c1=”Server01″; c2=”Server02″}
        C:\PS> $hash1 + $hash2

        Name                         Value
        —-                         —–
        c2                             Server02
        a                             1
        b                             2
        c1                             Server01
        c                             3

        C:\PS> $hash1 + 2
        You can add another hash table only to a hash table.
        At line:1 char:9
        + $hash1 + <<<< 2

        C:\PS> 2 + $hash1
        Cannot convert “System.Collections.Hashtable” to “System.Int32”.
        At line:1 char:4
        + 2 + <<<< $hash1

    The following examples demonstrate that you can add a hash table to an
    array. The entire hash table is added to the array as a single object.

        C:\PS> $array = 1,2,3
        C:\PS> $array + $hash1
        1
        2
        3

        Name                         Value
        —-                         —–
        a                             1
        b                             2
        c                             3

        C:\PS> $sum = $array + $hash1
        C:\PS> $sum.count
        4

        C:\PS> $sum[3]
        Name                         Value
        —-                         —–
        a                             1
        b                             2
        c                             3

        PS C:\ps-test> $sum + $hash2
        1
        2
        3

        Name                         Value
        —-                         —–
        a                             1
        b                             2
        c                             3
        c2                             Server02

    The following example shows that you cannot add hash tables that contain
    the same key:

        C:\PS> $hash1 = @{a=1; b=2; c=3}
        C:\PS> $hash2 = @{c=”red”}
        C:\PS> $hash1 + $hash2
        Bad argument to operator ‘+’: Item has already been added.
        Key in dictionary: ‘c’    Key being added: ‘c’.
        At line:1 char:9
        + $hash1 + <<<< $hash2

    Although the addition operators are very useful, use the assignment
    operators to add elements to hash tables and arrays. For more information
    see about_Assignment_Operators. The following examples use the +=
    assignment operator to add items to an array:

        C:\PS> $array
        1
        2
        3

        C:\PS> $array + “file”
        1
        2
        3
        file

        C:\PS> $array
        1
        2
        3

        C:\PS> $array += “file”
        C:\PS> $array
        1
        2
        3
        file

        C:\PS> $hash1

        Name                         Value
        —-                         —–
        a                             1
        b                             2
        c                             3

        C:\PS> $hash1 += @{e = 5}
        C:\PS> $hash1

        Name                         Value
        —-                         —–
        a                             1
        b                             2
        e                             5
        c                             3

    Windows PowerShell automatically selects the .NET Framework numeric type
    that best expresses the result without losing precision. For example:

        C:\PS> 2 + 3.1
        5.1
        C:\PS> (2). GetType().FullName
        System.Int32
        C:\PS> (2 + 3.1).GetType().FullName
        System.Double

    If the result of an operation is too large for the type, the type of the
    result is widened to accommodate the result, as in the following example:

        C:\PS> (512MB).GetType().FullName
        System.Int32
        C:\PS> (512MB * 512MB).GetType().FullName
        System.Double

    The type of the result will not necessarily be the same as one of the
    operands. In the following example, the negative value cannot be cast to an
    unsigned integer, and the unsigned integer is too large to be cast to
    Int32:

        C:\PS> ([int32]::minvalue + [uint32]::maxvalue).gettype().fullname
        System.Int64

    In this example, Int64 can accommodate both types.

    The System.Decimal type is an exception. If either operand has the Decimal
    type, the result will be of the Decimal type. If the result is too large
    for the Decimal type, it will not be cast to Double. Instead, an error
    results.

        C:\PS> [Decimal]::maxvalue
        79228162514264337593543950335
        C:\PS> [Decimal]::maxvalue + 1
        Value was either too large or too small for a Decimal.
        At line:1 char:22
        + [Decimal]::maxvalue + <<<< 1

    ARITHMETIC OPERATORS AND VariableS
    You can also use arithmetic operators with Variables. The operators act on
    the values of the Variables. The following examples demonstrate the use of
    arithmetic operators with Variables:

        C:\PS> $intA = 6
        C:\PS> $intB = 4
        C:\PS> $intA + $intB

        10

        C:\PS> $a = “Windows ”
        C:\PS> $b = “PowerShell ”
        C:\PS> $c = 2
    C:\PS> $a + $b + $c

        Windows PowerShell 2

    ARITHMETIC OPERATORS AND COMMANDS
    Typically, you use the arithmetic operators in expressions with numbers,
    strings, and arrays. However, you can also use arithmetic operators with
    the objects that commands return and with the properties of those objects.

    The following examples show how to use the arithmetic operators in
    expressions with Windows PowerShell commands:

    C:\PS> Get-Date
    Wednesday, January 02, 2008 1:28:42 PM

    C:\PS> $day = New-TimeSpan -day 1
    C:\PS> Get-Date + $day
    Thursday, January 03, 2008 1:34:52 PM

    C:\PS> Get-Process | where {($_.ws * 2) -gt 50mb}
    Handles NPM(K)    PM(K)     WS(K) VM(M) CPU(s)     Id ProcessName
    ——- ——    —–     —– —– ——     — ———–
     1896     39    50968     30620 264 1,572.55 1104 explorer
     12802     78 188468     81032 753 3,676.39 5676 OUTLOOK
        660     9    36168     26956 143    12.20    988 powershell
        561     14     6592     28144 110 1,010.09    496 services
     3476     80    34664     26092 234 …45.69    876 svchost
        967     30    58804     59496 416 930.97 2508 WINWORD

EXAMPLES
    The following examples show how to use the arithmetic operators in
    Windows PowerShell:

    C:\PS> 1 + 1
    2

    C:\PS> 1 – 1
    0

    C:\PS> -(6 + 3)
    -9

    C:\PS> 6 * 2
    12

    C:\PS> 7 / 2
    3.5

    C:\PS> 7 % 2
    1

    C:\PS> w * 3
    www

    C:\PS> 3 * “w”
    Cannot convert value “w” to type “System.Int32”. Error: “Input string was not
        in a correct format.”
    At line:1 char:4
    + 3 * <<<< “w”

    PS C:\ps-test> “Windows” + ” ” + “PowerShell”
    Windows PowerShell

    PS C:\ps-test> $a = “Windows” + ” ” + “PowerShell”
    PS C:\ps-test> $a
    Windows PowerShell

    C:\PS> $a[0]
    W

    C:\PS> $a = “TestFiles.txt”
    C:\PS> $b = “C:\Logs\”
    C:\PS> $b + $a
    C:\Logs\TestFiles.txt

    C:\PS> $a = 1,2,3
    C:\PS> $a + 4
    1
    2
    3
    4

    C:\PS> $servers = @{0 = “LocalHost”; 1 = “Server01”; 2 = “Server02”}
    C:\PS> $servers + @{3 = “Server03”}
    Name Value
    —- —–
    3 Server03
    2 Server02
    1 Server01
    0 LocalHost

    C:\PS> $servers
    Name Value
    —- —–
    2 Server02
    1 Server01
    0 LocalHost

    C:\PS> $servers += @{3 = “Server03”} #Use assignment operator
    C:\PS> $servers
    Name Value
    —- —–
    3 Server03
    2 Server02
    1 Server01
    0 LocalHost

SEE ALSO
    about_arrays
    about_Assignment_Operators
    about_Comparison_Operators
    about_hash_tables
    about_operators
    about_Variables
    Get-Date
    New-TimeSpan

about_aliases

TOPIC
    about_aliases

SHORT DESCRIPTION
    Describes how to use alternate names for cmdlets and commands in Windows
    PowerShell.

LONG DESCRIPTION
    An Alias is an alternate name or nickname for a cmdlet or for a command
    element, such as a Function, script, file, or executable file. You
    can use the Alias instead of the command name in any Windows PowerShell
    commands.

    To create an Alias, use the New-Alias cmdlet. For example, the following
    command creates the “gas” Alias for the Get-AuthenticodeSignature cmdlet:

        New-Alias -name gas -value Get-AuthenticodeSignature

    After you create the Alias for the cmdlet name, you can use the Alias
    instead of the cmdlet name. For example, to get the Authenticode signature
    for the SqlScript.ps1 file, type:

        Get-AuthenticodeSignature sqlscript.ps1

    Or, type:

        gas sqlscript.ps1

    If you create “word” as the Alias for Microsoft Office Word, you can type
    “word” instead of the following:

        “c:\program files\microsoft office\office11\winword.exe”

BUILT-IN AliasES
    Windows PowerShell includes a set of built-in Aliases, including “cd” and
    “chdir” for the Set-Location cmdlet, and “ls” and “dir” for the
    Get-ChildItem cmdlet.

    To get all the Aliases on the computer, including the built-in Aliases,
    type:

        Get-Alias

ALIAS CMDLETS
    Windows PowerShell includes the following cmdlets, which are designed for
    working with Aliases:

        – Get-Alias. Gets all the Aliases in the current session.
        – New-Alias. Creates a new Alias.
        – Set-Alias. Creates or changes an Alias.
        – Export-Alias. Exports one or more Aliases to a file.
        – Import-Alias. Imports an Alias file into Windows PowerShell.

    For detailed information about the cmdlets, type:

    Get-Help <cmdlet-name> -detailed

    For example, type:

    Get-Help Export-Alias -detailed

CREATING AN Alias
    To create a new Alias, use the New-Alias cmdlet. For example, to create the
    “gh” Alias for Get-Help, type:

    New-Alias -name gh -value Get-Help

    You can use the Alias in commands, just as you would use the full cmdlet
    name, and you can use the Alias with parameters.

    For example, to get detailed Help for the Get-WmiObject cmdlet, type:

    Get-Help Get-WmiObject -detailed

    Or, type:

    gh Get-WmiObject -detailed

SAVING AliasES
    The Aliases that you create are saved only in the current session. To use
    the Aliases in a different session, add the Alias to your Windows
    PowerShell profile. Or, use the Export-Alias cmdlet to save the Aliases to
    a file.

    For more information, type:

        Get-Help about_profile

GETTING AliasES
    To get all the Aliases in the current session, including the built-in
    Aliases, the Aliases in your Windows PowerShell profiles, and the Aliases
    that you have created in the current session, type:

    Get-Alias

    To get particular Aliases, use the Name parameter of the Get-Alias cmdlet.
    For example, to get Aliases that begin with “p”, type:

    Get-Alias -name p*

    To get the Aliases for a particular item, use the Definition parameter.
    For example, to get the Aliases for the Get-ChildItem cmdlet type:

    Get-Alias -definition Get-ChildItem

ALTERNATE NAMES FOR COMMANDS WITH PARAMETERS
    You can assign an Alias to a cmdlet, script, Function, or executable file.
    However, you cannot assign an Alias to a command and its parameters.
    For example, you can assign an Alias to the Get-EventLog cmdlet, but you
    cannot assign an Alias to the “Get-Eventlog -logname system” command.

    However, you can create a Function that includes the command. To create a
    Function, type the word “function” followed by a name for the Function.
    Type the command, and enclose it in braces ({}).

    For example, the following command creates the syslog Function. This
    Function represents the “Get-Eventlog -logname system” command:

    Function syslog {Get-Eventlog -logname system}

    You can now type “syslog” instead of the command. And, you can create
    Aliases for the syslog Function.

    For more information about Functions, type:

    Get-Help about_functions

ALIAS OBJECTS
     Windows PowerShell Aliases are represented by objects that are instances
     of the System.Management.Automation.AliasInfo class. For more information
     about this type of object, see “AliasInfo Class” in the Microsoft
     Developer Network (MSDN) library at
     http://go.microsoft.com/fwlink/?LinkId=143644.

     To view the properties and methods of the Alias objects, get the
     Aliases. Then, pipe them to the Get-Member cmdlet. For example:

    Get-Alias | Get-Member

     To view the values of the properties of a specific Alias, such as the
     “dir” Alias, get the Alias. Then, pipe it to the Format-List cmdlet. For
     example, the following command gets the “dir” Alias. Next, the command
     pipes the Alias to the Format-List cmdlet. Then, the command uses the
     Property parameter of Format-List with a wildcard character (*) to display
     all the properties of the “dir” Alias. The following command performs
     these tasks:

    Get-Alias -name dir | Format-List -property *

WINDOWS POWERSHELL Alias PROVIDER
    Windows PowerShell includes the Alias provider. The Alias provider lets you
    view the Aliases in Windows PowerShell as though they were on a file system
    drive.

    The Alias provider exposes the Alias: drive. To go into the Alias: drive,
    type:

    Set-Location Alias:

    To view the contents of the drive, type:

    Get-ChildItem

    To view the contents of the drive from another Windows PowerShell drive,
    begin the path with the drive name. Include the colon (:). For example:

    Get-ChildItem -path Alias:

    To get information about a particular Alias, type the drive name and
    the Alias name. Or, type a name pattern. For example, to get all the
    Aliases that begin with “p”, type:

    Get-ChildItem -path Alias:p*

    For more information about the Windows PowerShell Alias provider,
    type:

    Get-Help Alias-psprovider

SEE ALSO

    New-Alias
    Get-Alias
    Set-Alias
    Export-Alias
    Import-Alias
    Get-PSProvider
    Get-PSDrive
    about_functions
    about_profiles
    about_providers

Write-Warning

NAME
    Write-Warning

SYNOPSIS
    Writes a warning message.

SYNTAX
    Write-Warning [-Message] <string> [<CommonParameters>]

DESCRIPTION
    The Write-Warning cmdlet writes a warning message to the Windows PowerShell host. The response to the warning depends on the value of the user’s $WarningPreference Variable and the use of the WarningAction common parameter.

PARAMETERS
    -Message <string>
        Specifies the warning message.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.String
        You can pipe a string that contains the warning to Write-Warning.

OUTPUTS
    None
        Write-Warning writes only to the warning stream. It does not generate any other output.

NOTES

        The default value for the $WarningPreference Variable is “Continue”, which displays the warning and then continues executing the command. To determine valid values for a preference Variable such as $WarningPreference, set it to a string of random characters, such as “abc”. The resulting error message will list the valid values.

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

    C:\PS>Write-Warning “This is only a test warning.”

    Description
    ———–
    This command displays the message “WARNING: This is only a test warning.”

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

    C:\PS>$w = “This is only a test warning.”

    C:\PS> $w | Write-Warning

    Description
    ———–
    This example shows that you can use a pipeline operator (|) to send a string to Write-Warning. You can save the string in a Variable, as shown in this command, or pipe the string directly to Write-Warning.

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

    C:\PS>$warningpreference

    Continue

    C:\PS> Write-Warning “This is only a test warning.”
    This is only a test warning.

    C:\PS> $warningpreference = “SilentlyContinue”

    C:\PS> Write-Warning “This is only a test warning.”
    C:\PS>

    C:\PS> $warningpreference = “Stop”

    C:\PS> Write-Warning “This is only a test warning.”
    WARNING: This is only a test message.
    Write-Warning : Command execution stopped because the shell Variable “WarningPreference” is set to Stop.
    At line:1 char:14
    + Write-Warning <<<< “This is only a test message.”

    Description
    ———–
    This example shows the effect of the value of the $WarningPreference Variable on a Write-Warning command.

    The first command displays the default value of the $WarningPreference Variable, which is “Continue”. As a result, when you write a warning, the warning message is displayed and execution continues.

    When you change the value of the $WarningPreference Variable, the effect of the Write-Warning command changes again. A value of “SilentlyContinue” suppresses the warning. A value of “Stop” displays the warning and then stops execution of the command.

    For more information about the $WarningPreference Variable, see about_preference_variables.

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

    C:\PS>Write-Warning “This is only a test warning.” -WarningAction Inquire

    WARNING: This is only a test warning.

    Confirm
    Continue with this operation?
    [Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is “Y”):

    Description
    ———–
    This example shows the effect of the WarningAction common parameter on a Write-Warning command. You can use the WarningAction common parameter with any cmdlet to determine how Windows PowerShell responds to warnings resulting from that command. The WarningAction common parameter overrides the value of the $WarningPreference only for that particular command.

    This command uses the Write-Warning cmdlet to display a warning. The WarningAction common parameter with a value of “Inquire” directs the system to prompt the user when the command displays a warning.

    For more information about the WarningAction common parameter, see about_CommonParameters.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113430
    about_preference_variables
    about_CommonParameters
    Write-Debug
    Write-Error
    Write-Host
    Write-Output
    Write-Progress
    Write-Verbose

Write-Verbose

NAME
    Write-Verbose

SYNOPSIS
    Writes text to the verbose message stream.

SYNTAX
    Write-Verbose [-Message] <string> [<CommonParameters>]

DESCRIPTION
    The Write-Verbose cmdlet writes text to the verbose message stream in Windows PowerShell. Typically, the verbose message stream is used to deliver information about command processing that is used for debugging a command.

    By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference Variable or using the Verbose common parameter in any command.

PARAMETERS
    -Message <string>
        Specifies the message to display. This parameter is required. You can also pipe a message string to Verbose-Message.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.String
        You can pipe a string that contains the message to Write-Verbose.

OUTPUTS
    None
        Write-Verbose writes only to the verbose message stream.

NOTES

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

    C:\PS>Write-Verbose -Message “Searching the Application Event Log.”

    C:\PS> Write-Verbose -Message “Searching the Application Event Log.” -Verbose

    Description
    ———–
    These commands use the Write-Verbose cmdlet to display a status message. By default, the message is not displayed.

    The second command uses the Verbose common parameter, which displays any verbose messages, regardless of the value of the $VerbosePreference Variable.

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

    C:\PS>$VerbosePreference = “Continue”

    C:\PS> Write-Verbose “Copying file $filename”

    Description
    ———–
    These commands use the Write-Verbose cmdlet to display a status message. By default, the message is not displayed.

    The first command assigns a value of “Continue” to the $VerbosePreference preference Variable. The default value, “SilentlyContinue”, suppresses verbose messages.

    The second command writes a verbose message.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113429
    Write-Error
    Write-Warning
    about_preference_variables