PowerShell Arithmetic Operators

In this tutorial we will see about PowerShell Arithmetic Operators and how PowerShell is dealing with them. Some of the arithmetic operators are working also on string values. Below we will go through those operators with examples to understand their use. Below is the list of PowerShell arithemtic operators.

Operators:

+ : Adds integers and concatenates stings

: Adds integers and concatenates stings - : Subtracts a value from another. Makes a number a negative number.

: Subtracts a value from another. Makes a number a negative number. * : Multiplies numbers. Copies strings and arrays by the specified number.

: Multiplies numbers. Copies strings and arrays by the specified number. / : Divides two values.

: Divides two values. % : Returns the remainder of a division operator

: Returns the remainder of a division operator -band : Bitwise AND

: Bitwise AND -bnot : Bitwise NOT

: Bitwise NOT -bor : Bitwise OR

: Bitwise OR -bxor : Bitwise XOR

: Bitwise XOR -shl : Shifts bits to the left by the specified number of times.

: Shifts bits to the left by the specified number of times. -shr : Shifts bits to the right by the specified number of times.

: Shifts bits to the right by the specified number of times. ( ) : Parentheses

Arithmetic operators are working on 64-bit numbers from PowerShell 2.0 and onward. -shl and -shr were introduced in PowerShell 3.0. There is a specific order that PowerShell processes the arithmetic operators. The order is the same with Mathematics. Below you are able to see the order of processing the operators:

( ) – Parentheses - – Negative number or unary operator * , / , % – Multiplication , Division + , - – Addition, Substaction -shl , -shr – Shift-left , Shift-right -band – Bitwise AND -bnot – Bitwise NOT -bxor – Bitwise XOR -bor – Bitwise OR

Please note that all operations within your scripts will be performed in that order. You need to be careful when you are writing your expressions as it can affect the results.

Code:

PS S:\> (2 + 3) * 5 25

[adinserter name=”In Article”]

+ , – , * , / , %

Lets see few examples of arithmetic operators.

Code:

PS S:\> 5 + 5 10 PS S:\> 5 - 3 2 PS S:\> 5 * 5 25 PS S:\> 5 / 5 1 PS S:\> 7 % 2 1

The above operators are easy to understand. Now lets see the rest of the operators and there results.

Binary Arithmetic Operators

They work the same with logical operators for binary numbers. I will give some examples and basic binary details on how this works but I will not go in to much details. Lets see the general rules for bits.

AND

0 AND 0 = 0

0 AND 1 = 0

1 AND 0 = 0

1 AND 1 = 1

OR

0 OR 0 = 0

0 OR 1 = 1

1 OR 0 = 1

1 OR 1 = 1

NOT

NOT 0 = 1

NOT 1 = 0

XOR

0 XOR 0 = 0

0 XOR 1 = 1

1 XOR 0 = 1

1 XOR 1 = 0

[adinserter name=”In Article”]

The below examples will help you understand.

-band

The numbers that I will use are below including their binary format.

5 = 101

1 = 001

2 = 010

Code:

PS S:\> 5 -band 5 5 PS S:\> 5 -band 1 1 PS S:\> 5 -band 2 0

If we take the second example the process in binary is the below

5 = 101

1= 001

Result = 001

-bnot

Code:

PS S:\> -bnot 5 -6

-bor

Code:

PS S:\> 5 -bor 2 7 PS S:\> 5 -bor 3 7

Let see why we have the same result:

Example 1:

5 : 00000101

2 : 00000010

7: 00000111 (Result)

Example 2:

5 : 00000101

3 : 00000011

7: 00000111 (Result)

-bxor

Code:

PS S:\> 5 -bxor 2 7 PS S:\> 5 -bxor 3 6

Example 1:

5 : 00000101

2 : 00000010

7: 00000111 (Result)

Example 2:

5 : 00000101

3 : 00000011

7: 00000110 (Result)

-shl

Code:

PS S:\> 9 -shl 2 36

9 : 00001001

36: 00100100 (Result)

-shr

Code:

PS S:\> 9 -shr 2 2

9 : 00001001

2 : 00000010 (Result)

[adinserter name=”In Article”]

Rounding

There are times that we would like our results to be always an integer number. There is a way to perform this in PowerShell. In case the decimal in the result is 0.5, PowerShell rounds the number to the nearest even integer. The below examples will help you understand.

Code:

PS S:\> [int](1 / 2) 0 PS S:\> [int](3 / 2) 2 PS S:\> [int](5 / 2) 2 PS S:\> [int](7 / 2) 4 PS S:\> [int](9 / 2) 4 PS S:\> [int](11 / 2) 6

Adding and Multiplying Strings

Now lets see how addition ( + ) and multiplication ( * ) work with strings. I have used the below code for my examples.

Code:

$a = "The time is: " $b = Get-Date -Format HH:mm $a + $b $a = Get-Date -Format HH:mm $b = "The time is: $a ....." $b * 5 $a = @("a","b","c","d","e") $a * 5

Results:

Note that you can combine not only plain string values using the ( + ) operator but also hash tables. You have to note though that if there is any conflict in the keys of the hashes then it will throw an error. You cannot combine 2 hashes that they include same key.

Example:

PS S:\> $a = @{a=1;b=2;c=3} PS S:\> $b = @{d=4;e=5} PS S:\> $a + $b Name Value ---- ----- c 3 e 5 a 1 d 4 b 2 PS S:\>

I hope the tutorial about PowerShell Arithmetic Operators is helpful.

Please let me know your comments and thoughts. You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

[adinserter name=”Matched-Content”]