Windows PowerShell includes special
operators that you can use to perform tasks that cannot be performed by
the other operators. Special operators in Windows PowerShell allow you
to perform tasks such as dot-sourcing, creating arrays, and more. Table 1 lists the special operators that are available.
Table 1. Windows PowerShell Special Operators
Operator | Name | Description |
---|
.. | Range operator | Indicates a range of values; the first value in the range goes before the operator and the last value goes after it |
& | Call operator | Runs a command, script, or script block |
. | Dot-sourcing operator | Runs a script and includes the items, functions, and variables in the script to the current scope |
:: | Static member operator | Calls the static properties operator and methods of a .NET Framework class |
-f | Format operator | Formats strings by using the format method of string objects |
$() | Subexpression operator | Returns the result of an expression placed in a parenthetical list |
@() | Array subexpression operator | Returns one or more statements as an array |
, | Comma operator | As a binary operator, creates an array; as a unary operator, creates an array with one member |
The range operator (..) is used to retrieve a specified range from an array.
PS > $array = 1,2,3,4,5
PS > $array[0..2]
1
2
3
This example retrieves the elements with index 0 to 2.
The range operator also accepts negative ranges.
The call operator (&) in Windows PowerShell is
used to run commands, scripts, or script blocks. One thing to keep in
mind is that the call operator does not interpret parameters. This
example demonstrates how to run a command stored in a variable and
represented by a string:
PS > $myCommand = "Get-SPWeb"
PS > & $myCommand -Identity http://SPServer01
Url
---
http://spserver01
Here, we use "Get-SPWeb" as the input string to the variable and specify the arguments when we call the variable.
You can also use the call operator to run script blocks. A script block can contain any amount of code and is defined by braces.
The dot-sourcing operator (.) is used to include
variables and functions from a script to the current scope. This means
that you can store functions and variables in a script and use the
dot-source notation to quickly access the functions and variables
contained in the script. Here is an example:
PS > $variableInScript
PS > . .\myScript.ps1
PS > $variableInScript
This variable is placed in the myScript.ps1 script
Here, we first try to call the variable $variableInScript. Since we have not created a variable called $variableInScript in our current session, the command does not return a value. Next, we dot-source our script, which contains the variable $variableInScript. When we call the variable a second time, a value is returned.
The static member operator (::) is used to call
static methods and properties of a .NET Framework class. To find out
static methods and properties of a class, use the Get-Member cmdlet with the Static switch parameter.
PS > [System.Math] | Get-Member -Static
Here are some examples of using the static member operator with the static methods and properties of the System.Math class:
Use the static method Pow() to return the specified number raised to the specified power:
PS > [System.Math]::Pow(5,5)
3125
Use Sqrt() to calculate the square root of 9:
PS > [System.Math]::Sqrt(9)
3
Call the static property PI, which represents the ratio of a circle to its diameter:
PS > [System.Math]::PI
3,14159265358979
The format operator (-f) is a binary operator that uses the same formatting rules as the Format() method in the .NET Framework. It takes a string on the left side and an array of values on the right side. Here is an example:
PS > "{0}" -f "PowerShell"
PowerShell
In this example, the value enclosed in braces
represents the index of the element on the right side. Since the format
operator allows an array on the right side, you can use more values.
PS > "{0}" -f "PowerShell","Windows"
PowerShell
With two elements on the right side but one on the
left, the operator returns only the first element. If you add a second
value on the left side, you can retrieve both values:
PS > "{0} {1}" -f "PowerShell","Windows"
PowerShell Windows
You can also switch the places of the numeric values to change the output.
PS > "{1} {0}" -f "PowerShell","Windows"
Windows PowerShell
The subexpression operator ($()) returns
the result of one or more statements. If the result contains multiple
values, an array is returned. The next example demonstrates how to use
the subexpression operator.
PS > "There are: $((Get-Command -Noun SP*).Count) SharePoint cmdlets available
in Windows PowerShell"
There are: 531 SharePoint cmdlets available in Windows PowerShell
The array subexpression operator (@())
returns the result of one or more statements as an array. You can use
the operator to create a simple array, as shown in this example:
PS > @("Windows","PowerShell")
Windows
PowerShell
You can access elements in the array by their index.
PS > @("Windows","PowerShell")[0]
Windows
PS > @("Windows","PowerShell")[1]
PowerShell
The comma operator (,) is a binary operator used to create simple arrays in Windows PowerShell.
You can also use the comma operator to place a single line in an array.
PS > $array = ,"PowerShell"
PS > $array[0]
PowerShell
When you place a comma operator in front of
a value, Windows PowerShell treats the value as an element in an array.
When you place the statement in a variable and call the first element
in the array, the string value is returned.