Any suggestions? Export (0) Print
Expand All

User-defined variables


Applies to: Exchange Server 2013

A variable is a location to store information. Unlike in many programming environments, in the Exchange Management Shell, you don't have to declare a variable before you use it.

You designate a variable by prepending a string with a dollar sign ($). You must enclose the string in braces ({ }) if the string contains spaces or other special characters. By using the array reference notation ([ ]), you can address the elements of an array or hash table variable. For more information, see Arrays.

See the following sections for more information about user-defined variables in the Shell:

Using variables to store values

Storing the output of a command in a variable

Storing the output of the Dir command in a variable

Variables are very useful if you want to store a value. You can assign values to variables by using an assignment operator. For more information about operators, see Syntax.

For example, to assign a value of 8 to the variable $Example, use the following command:

$Example = 8

This command assigns the integer 8 to the variable $Example. You can then call the $Example variable later in another command to recall the value. The values that are specified in a variable are treated exactly as if the value that it contains was typed in the location that the variable is specified. For example, the following two commands are equivalent if $Example2 is assigned the value "Hello":

Write-Host $Example2
Write-Host "Hello"

You can also store the output of commands in a variable for later use. When you assign a command to a variable, the command is evaluated at the time that command is run. The output of that command is assigned to the variable. For example, if you run $CurrentDate = Get-Date on the command line and then call $CurrentDate repeatedly over several seconds, the value that is reported is the same every time that the variable is called.

When you assign the output of a command to a variable, you can also access the properties and methods of the underlying object. For example, to view the properties and methods that are available when you assign Get-Date to $CurrentDate, you can use the $CurrentDate | Get-Member command. When you use the $CurrentDate | Get-Member -MemberType Property command, the following properties are returned in a list:

Name        MemberType Definition
Date        Property   System.DateTime Date {get;}
Day         Property   System.Int32 Day {get;}
DayOfWeek   Property   System.DayOfWeek DayOfWeek {get;}
DayOfYear   Property   System.Int32 DayOfYear {get;}
Hour        Property   System.Int32 Hour {get;}
Kind        Property   System.DateTimeKind Kind {get;}
Millisecond Property   System.Int32 Millisecond {get;}
Minute      Property   System.Int32 Minute {get;}
Month       Property   System.Int32 Month {get;}
Second      Property   System.Int32 Second {get;}
Ticks       Property   System.Int64 Ticks {get;}
TimeOfDay   Property   System.TimeSpan TimeOfDay {get;}
Year        Property   System.Int32 Year {get;}

You can then call any of these properties by typing the variable, a period (.), and then the property that you want to view. For example, to view the year that is stored on a variable, use the following command:


By accessing the properties of a variable, you can easily manipulate and use each piece of information that is stored in the variable without the use of text parsing.

You can also store the output of the Dir command in a variable. Because the Dir command returns multiple rows when it runs, each row that is returned is stored in a variable as a new array element. You can then access each file object that is stored in the newly created array. For more information about arrays, see Arrays.

The following command assigns the output of the Dir command to the $DirOutput variable:

$DirOutput = Dir

You can then select a specific file object by specifying the array index that you want to view as follows:


Or you can create a simple loop that cycles through the whole array and displays the name and file size of each file that is stored in the array as follows:

0..$DirOutput.Length | ForEach { $DirOutput[$_].Name + " is " + $DirOutput[$_].Length + " bytes long." }

The following list examines this example:

  • The 0..$DirOutput.Length command instructs the Shell to output an integer from 0 to the maximum length of the array that is stored in the $DirOutput variable.

  • The output of the 0..$DirOutput.Length command is piped to the ForEach command that loops through each element of the array until it reaches the end of the array. The ForEach command runs the commands that are enclosed in the braces " { } ".

  • The $_ variable stores the current object that is in the pipeline. In this case, the object in the pipeline is an integer that is produced by the 0..$DirOutput.Length command as it counts from 0 to the maximum length of the array. This variable is used in the $DirOutput[$_].Name command and $DirOutput[$_].Length command to select the array element to access.

  • For more information about the $_ variable, see Shell variables.

  • The plus " + " signs concatenate the output of the $DirOutput[$_].Name command and $DirOutput[$_].Length command, together with the strings supplied, to create output similar to the following:

    abv_dg.dll is 416144 bytes long.
    addxa.dll is 285056 bytes long.
    ASDat.MSI is 5626880 bytes long.
    ASEntDat.MSI is 5626880 bytes long.
    ASEntIRS.MSI is 910336 bytes long.
    ASEntSig.MSI is 45056 bytes long.
    BPA.Common.dll is 211848 bytes long.
    BPA.ConfigCollector.dll is 101272 bytes long.
    BPA.NetworkCollector.dll is 52128 bytes long.

These examples show that you can use the Length property more than one time to display different information about the same variable. You can do this because more than one type of data is stored in the $DirOutput variable. The first type of data is the directory object itself, and the second type of data is the file objects. When you run the $DirObject.Length command, without specifying an array index, you're accessing the directory parent object types that are stored in the array. When you specify an array index, such as $DirObject[5].Length, you're accessing the file child objects that are stored in the directory object.

This behavior exists on many objects. You can typically access many levels of object data that are contained in a single variable. The ability to access this data makes the Shell quite flexible.

© 2016 Microsoft