SoFunction
Updated on 2025-03-10

Introduction to some common special operators in PowerShell

&, call the operator.

Syntax: & <executable file path> [<parameter list>]
For example:

Copy the codeThe code is as follows:
$execPath="D:\Progra~1\FlashFXP\"
$execArgs="-upload ftp://u:p@ip:21 "
$execArgs=$execArgs+"-remotepath=`"/`" "
$execArgs=$execArgs+"-localpath=`"d:\123\`" "
& $execPath $()

This is an example of how we called flashfxp to upload files before, which is to use the & call character.

Point (.), multifunctional operator.

1. Call external PS1 script file

Copy the codeThe code is as follows:
. D:\PowerShell\test.ps1

2. Member operators

Copy the codeThe code is as follows:
$object.properties; or $object.method()

3. Path operator
One point (.) represents the current directory, and two points (..) represent the upper directory.
For example: .\test.ps1, ..\PowerShell\test.ps1, these two expressions refer to themselves relative to D:\PowerShell\test.ps1.

Two points (..), range operator.

This is exactly the same as the two points above representing the upper directory, but the use is different.
"1..5" means 1 to 5, and "5..1" means 5 to 1.
When we are programming, we may use similar usages when using foreach:

Copy the codeThe code is as follows:
foreach($i in 1..5){
Write-Host $i;
}

Double colon (::), static member operator.

When programming PowerShell, classes, their properties and methods in .NET are often used. For example, if the () method is called in PowerShell, it is called in the following way.

Copy the codeThe code is as follows:
[]::Sleep(10000);

(-f), format operator.

Syntax: Format string -f value 1, value 2,...
Example: "{0} {1:N} {2,-10}" -f 10000,[math]::pi,"good"

$(), subexpression operator.

Calculating the value of an expression in brackets is a bit like the eval() function. If the value is a result, the scalar is returned. If the values ​​are multiple, an array is returned.
For example:

Copy the codeThe code is as follows:
$i = 200;
#Return value 1000
$($i*5);
#Return an array of objects on a logical disk
$(Get-WMIObject win32_LogicalDisk)

@(), array subexpression operator.

Return the result of one or more statements through an array. If there is only one item, the array has only one member. like:

Copy the codeThe code is as follows:
@(Get-WMIObject win32_LogicalDisk)

Comma (,) array element operator.

When this operator is used as a binary operator, commas are used to create an array, listing the individual elements of the array. like:

Copy the codeThe code is as follows:
$array = "a","b","c","d"

When used as a unary operator, commas are used to create an array containing only one member. like:
Copy the codeThe code is as follows:
$singleArray = ,"a"