Simple Operators

Once you have defined your data, the next step is to work with it.

Arithmetic Operators

The arithmetic operators allow you to perform mathematical operations on your data.

Windows PowerShell arithmetic operators

Operator

Meaning

+

The addition operator:

$leftValue + $rightValue

When used with numbers, returns their sum.

When used with strings, returns a new string created by appending the second string to the first.

When used with arrays, returns a new array created by appending the second array to the first.

When used with hashtables, returns a new hashtable created by merging the two hashtables. Since hashtable keys must be unique, PowerShell returns an error if the second hashtable includes any keys already defined in the first hashtable.

When used with any other type, PowerShell uses that type’s addition operator (op_Addition) if it implements one.

-

The subtraction operator:

$leftValue - $rightValue

When used with numbers, returns their difference.

This operator does not apply to strings.

This operator does not apply to arrays.

This operator does not apply to hashtables.

When used with any other type, PowerShell uses that type’s subtraction operator (op_Subtraction) if it implements one.

*

The multiplication operator:

$leftValue * $rightValue

When used with numbers, returns their product.

When used with strings ("Test" * 5), returns a new string created by appending the string to itself the number of times you specify.

When used with arrays (1..3 * 7), returns a new array created by appending the array to itself the number of times you specify.

This operator does not apply to hashtables.

When used with any other type, PowerShell uses that type’s multiplication operator (op_Multiply) if it implements one.

/

The division operator:

$leftValue / $rightValue

When used with numbers, returns their quotient (in floating-point format.)

This operator does not apply to strings.

This operator does not apply to arrays.

This operator does not apply to hashtables.

When used with any other type, PowerShell uses that type’s multiplication operator (op_Division) if it implements one.

%

The modulus operator:

$leftValue % $rightValue

When used with numbers, returns the remainder of their division.

This operator does not apply to strings.

This operator does not apply to arrays.

This operator does not apply to hashtables.

When used with any other type, PowerShell uses that type’s multiplication operator (op_Modulus) if it implements one.

+=

-=

*=

/=

%=

Assignment operators:

$variable <operator>= value

These operators match the simple arithmetic operators (+, -, *, /, and %) but store the result in the variable on the lefthand side of the operator. It is a short-form for

$variable = $variable <operator> value

Tip

The System.Math class in the .NET Framework offers many powerful operations in addition to the native operators supported by PowerShell:

PS >[Math]::Pow([Math]::E, [Math]::Pi)
23.1406926327793

See the section named “Working with the .NET Framework” to learn more about using PowerShell to interact with the .NET Framework.

Logical Operators

The logical operators allow you to compare Boolean values.

Windows PowerShell logical operators

Operator

Meaning

-and

Logical AND:

$leftValue -and $rightValue

Returns $true if both lefthand and righthand arguments evaluate to $true. Returns $false otherwise.

You can combine several –and operators in the same expression:

$value1 –and $value2 –and $value3

PowerShell implements the –and operator as a short-circuit operator, by evaluating arguments only when all arguments preceding it evaluate to $true.

-or

Logical OR:

$leftValue -or $rightValue

Returns $true if the lefthand or righthand arguments evaluate to $true. Returns $false otherwise.

You can combine several –or operators in the same expression:

$value1 –or $value2 –or $value3

PowerShell implements the –or operator as a short-circuit operator – by evaluating arguments only when all arguments preceding it evaluate to $false.

-xor

Logical Exclusive OR:

$leftValue -xor $rightValue

Returns $true if either the lefthand or righthand argument evaluates to $true, but not if both do. Returns $false otherwise.

-not

!

Logical NOT:

-not $value

Returns $true if its (only) righthand argument evaluates to $false. Returns $false otherwise.

Binary Operators

The binary operators allow you to apply the Boolean logical operators bit by bit to the operator’s arguments. When comparing bits, a 1 represents $true, while a 0 represents $false.

Windows PowerShell binary operators

Operator

Meaning

-band

Binary AND:

$leftValue -band $rightValue

Returns a number where bits are set to 1 if the bits of the lefthand and righthand arguments at that position are both 1. All other bits are set to 0.

For example:

PS >$boolean1 = "110110110"
PS >$boolean2 = "010010010"
PS >$int1 = [Convert]::ToInt32($boolean1, 2)
PS >$int2 = [Convert]::ToInt32($boolean2, 2)
PS >$result = $int1 -band $int2
PS >[Convert]::ToString($result, 2)
10010010

-bor

Binary OR:

$leftValue -bor $rightValue

Returns a number where bits are set to 1 if either of the bits of the lefthand and righthand arguments at that position are 1. All other bits are set to 0.

For example:

PS >$boolean1 = "110110110"
PS >$boolean2 = "010010010"
PS >$int1 = [Convert]::ToInt32($boolean1, 2)
PS >$int2 = [Convert]::ToInt32($boolean2, 2)
PS >$result = $int1 -bor $int2
PS >[Convert]::ToString($result, 2)
110110110

-bxor

Binary Exclusive OR:

$leftValue -bxor $rightValue

Returns a number where bits are set to 1 if either of the bits of the lefthand and righthand arguments at that position are 1, but not if both are. All other bits are set to 0.

For example:

PS >$boolean1 = "110110110"
PS >$boolean2 = "010010010"
PS >$int1 = [Convert]::ToInt32($boolean1, 2)
PS >$int2 = [Convert]::ToInt32($boolean2, 2)
PS >$result = $int1 -bor $int2
PS >[Convert]::ToString($result, 2)
100100100

-bnot

Binary NOT:

-bnot $value

Returns a number where bits are set to 1 if the bit of the righthand (and only) argument at that position is set to 1. All other bits are set to 0.

For example:

PS >$boolean1 = "110110110"
PS >$int1 = [Convert]::ToInt32($boolean1, 2)
PS >$result = -bnot $int1
PS >[Convert]::ToString($result, 2)
11111111111111111111111001001001

Other Operators

PowerShell supports several other simple operators.

Other Windows PowerShell operators

Operator

Meaning

-replace

The replace operator:

target” –replace “pattern”,”replacement

Returns a new string, where the text in "target" that matches the regular expression "pattern" has been replaced with the replacement text, "replacement"

By default, PowerShell performs a case-insensitive comparison. The –ireplace operator makes this case insensitivity explicit, while the –creplace operator performs a case-sensitive comparison.

If the regular-expression pattern contains named captures or capture groups, the replacement string may reference those as well.

For example:

PS >"Hello World" -replace "(.*) (.*)",'$2 $1'
World Hello

For more information on the details of regular expressions, see the “Regular Expression Reference” appendix.

-f

The format operator:

<Format String> -f <Value Array>

Returns a string, where the format items in the format string have been replaced with the text equivalent of the values in the value array.

For example:

PS >"{0:n0}" -f 1000000000
1,000,000,000

The format string for the format operator is exactly the format string supported by the .NET String.Format method.

For more details about the syntax of the format string, see the “.NET String Formatting” appendix.

-as

The type conversion operator:

$value –as [Type]

Returns $value cast to the given .NET type. If this conversion is not possible, PowerShell returns $null.

For example:

PS >3/2 -as [int]
2
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.84.155