• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Arithmetic Operators

PowerShell Arithmetic Operators

18/05/2018 by Stephanos 1 Comment

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
  • - : Subtracts a value from another. Makes a number a negative number.
  • * : Multiplies numbers. Copies strings and arrays by the specified number.
  • / : Divides two values.
  • % : Returns the remainder of a division operator
  • -band : Bitwise AND
  • -bnot : Bitwise NOT
  • -bor : Bitwise OR
  • -bxor : Bitwise XOR
  • -shl : Shifts bits to the left by the specified number of times.
  • -shr : 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:

  1. ( ) – Parentheses
  2. - – Negative number or unary operator
  3. * , / , % – Multiplication , Division
  4. + , - – Addition, Substaction
  5. -shl , -shr – Shift-left , Shift-right
  6. -band – Bitwise AND
  7. -bnot – Bitwise NOT
  8. -bxor – Bitwise XOR
  9. -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:

PowerShell Arithmetic Operators

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:

  • PowerShell Tutorials
  • PowerShell Scripts
  • PowerShell Logical Operators
  • about_Arithmetic_Operators | Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Arithmetic Operators
Article Name
PowerShell Arithmetic Operators
Description
PowerShell Arithmetic Operators. In this tutorial you will learn how PowerShell processes arithmetic operators and how you are able to use them. Stephanos Constantinou Blog - PowerShell Scripting
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Arithmetic Operators, Get-Date, PowerShell Operators

Reader Interactions

Trackbacks

  1. PowerShell Arithmetic Operators - How to Code .NET says:
    18/05/2018 at 20:00

    […] on May 17, 2018by admin submitted by /u/SConstantinou [link] [comments] No comments […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact