Operators $( ) @( ) :: &

( ) Expression operator.

Brackets work just as they do in mathematics, each pair of brackets returns the result of the expression.

$( ) SubExpression operator.

Unlike simple brackets, a subexpression can contain multiple ; semicolon ; separated ; statements.

The output of each statement contributes to the output of the subexpression. For a single result, it will return a scalar. For multiple results, it will return an array.

@( ) Array SubExpression operator.

An array subexpression behaves just like a subexpression except that it guarantees that the output will be an array.
This works even if there is no output at all (gives an empy array.)
If the result is a scalar value then the result will be a single element array containg the scalar value.

(If the output is already an array then the use of an array subexpression will have no effect, it won't wrap one array inside of another array.)

n.b. Using either of the operators above will cause the powershell parser to re-evaluate the parsing mode based on the first non-whitespace character inside the parenthesis. A neat effect of this is that object properties will be evaluated instead of being treated as literal strings:

"$user.department" ==> JDOE.department
"$($user.department)" ==> "Human Resources"

Subexpressions allow you to evaluate and act on the results of an expression in a single line; with no need for an intermediate variable:

if($(code that returns a value/object) -eq "somevalue") { do_something }

:: Static member operator

Call the static properties operator and methods of a .NET Framework class.
To find the static properties and methods of an object, use the -Static parameter of Get-Member:

[datetime] | gm -static
[datetime]::now
[datetime]::Utcnow

, Comma operator

As a binary operator, the comma creates an array.
As a unary operator, the comma creates an array with one member. Place the comma before the member.

& Call operator

Run a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters.

C:\PS> $c = "get-executionpolicy"
C:\PS> $c
get-executionpolicy
C:\PS> & $c
AllSigned

. Dot sourcing operator

Run a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope. (without dot sourcing, the variables created within a script will all disappear when the script finishes.)

. C:\sample1.ps1
. .\sample2.ps1

Note: The dot sourcing operator is followed by a space. Use the space to distinguish the dot from the dot (.) symbol that represents the current directory.

-f Format operator

Format strings.

Enter the format string(s) on the left side of the operator and the object(s) to be formatted on the right side.

"{I,A:FS} {I,A:FS} {I,A:FS}.." -f "string0", "string1", "string2"...

Key:

I = Index of the item to display, 0=string0, 1=string1 etc. (this is equivalent to tokens in the FOR /F command in Windows CMD)

A = Alignment. A positive number = right align n characters.
A negative number = left align n characters.
so {2,-25} will allocate 25 characters of horizontal space on the line, even if the string is only 1 character long.

FS = an optional format string that acts on the item depending on its type.
Valid format strings (not case sensitive):

:c Currency format
:e Scientific (exp) notation
:f Fixed point
:f5 = fix to 5 places
:g Most compact format, fixed or sci
:g5 = 5 significant digits
:n include culture separator for thousands 1,000.00
:p percentage
:r reversible precision
:x Hex format
:hh
:mm
:ss
Convert a DateTime to a 2 digit Hour/minute/second
"{0:hh:0:mm}"
:ddd Convert a DateTime to Day of the Week

A full list of Date Formats
You can also include static text before or in-between the -f {format strings.}

..Range operator

Produce a sequence of numbers:

10..20
5..25

Examples:

PS C:\> $($x * 64)
PS C:\> $(Get-WMIObject win32_Directory)
PS C:\> @(Get-WMIObject win32_logicalDisk)
PS C:\> "{1,10} {0,10} {2,10:x}" -f "First", "Second", 255
    Second     First        FF

PS C:\> "{0} {1,-10} {2:N}" -f 1, "hello",[math]::pi
1 hello 3.14

“I skate to where the puck is going to be, not where it has been” - Wayne Gretsky

Related:

Pipelines - Pass objects down the pipeline
Variables - Powershell Variables (int, String)
Unicode U+2200 to U+22FF - basic Mathematical Operators



Back to the Top

© Copyright SS64.com 1999-2012
Some rights reserved