PowerShell Arithmetic Operators [With Examples]

Recently, I worked on a PowerShell script that involved some addition and subtraction operations. I tried different examples. In this tutorial, I will show you how to use PowerShell arithmetic operators with examples.

PowerShell provides a range of arithmetic operators to perform basic calculations. These operators include addition (+), subtraction (-), multiplication (*), division (/), etc.

PowerShell Arithmetic Operators

Arithmetic operators in PowerShell are used to perform mathematical operations on numeric values. These operators handle addition, subtraction, multiplication, and division, making it easier to manipulate numeric data in scripts and commands.

PowerShell supports a variety of arithmetic operators. These include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). The addition and multiplication operators can also work with strings and arrays. For example:

  • Addition (+): Adds numbers, concatenates strings and arrays.
  • Subtraction (-): Subtracts one number from another.
  • Multiplication (*): Multiplies numbers, repeats strings and arrays.
  • Division (/): Divides one number by another.
  • Modulus (%): Finds the remainder of a division operation.

In PowerShell, operator precedence determines the order in which parts of an expression are evaluated. The basic rules are like the below:

  1. Multiplication (*) and Division (/) are evaluated before addition (+) and subtraction (-).
  2. Modulus (%) also follows the same precedence as multiplication and division.
  3. To override these rules, use parentheses. Expressions inside parentheses are evaluated first.

Read PowerShell -contains Operator

Basic Arithmetic Operators in PowerShell

Now, let me show you some basic arithmetic operators in PowerShell with examples.

Addition (+)

The addition operator in PowerShell adds two numbers together. It’s one of the most straightforward operations in PowerShell.

Here is an example.

$a = 5
$b = 10
$result = $a + $b
Write-Output $result  # Output: 15

I executed the above script using VS code, and you can see the output in the screenshot below:

PowerShell Arithmetic Operators

Subtraction (-)

The PowerShell subtraction operator subtracts the right-hand operand from the left-hand operand.

Here is an example.

$a = 15
$b = 5
$result = $a - $b
Write-Output $result  # Output: 10

Multiplication (*)

The multiplication operator in PowerShell multiplies two numbers.

Here is an example.

$a = 4
$b = 3
$result = $a * $b
Write-Output $result  # Output: 12

Division (/)

The division operator in PowerShell divides the left-hand operand by the right-hand operand. Note that division by zero will result in an error.

Here is an example.

$a = 20
$b = 4
$result = $a / $b
Write-Output $result  # Output: 5

Modulus (%)

The modulus operator (%) in PowerShell returns the remainder of a division operation.

You can see an example below:

$a = 10
$b = 3
$result = $a % $b
Write-Output $result  # Output: 1

This operator helps in determining even or odd numbers by checking if a number modulo 2 equals zero:

$number = 4
if ($number % 2 -eq 0) {
    Write-Output "Even"  # Prints "Even"
} else {
    Write-Output "Odd"
}

You can see the output in the screenshot below, after I executed the above PowerShell script. You will also get the exact output:

PowerShell Arithmetic Operators Examples

Read PowerShell Like Operator

Advanced Arithmetic Operators in PowerShell

Now, let me show you a few advanced arithmetic operators in PowerShell.

Increment (++)

In PowerShell, the increment operator increases a variable’s value by one.

You can see an example below.

$a = 5
$a++
Write-Output $a  # Output: 6

Decrement (–)

The decrement operator in PowerShell decreases a variable’s value by one.

Here is an example.

$a = 5
$a--
Write-Output $a  # Output: 4

Exponentiation (**)

The exponentiation operator in PowerShell raises a number to the power of another number. You can see an example below:

$a = 2
$b = 3
$result = $a ** $b
Write-Output $result  # Output: 8

Combine Arithmetic Operators in PowerShell

PowerShell allows you to combine multiple arithmetic operations in a single expression. The order of operations follows the standard mathematical rules (PEMDAS/BODMAS). Here is an example, you can see.

$a = 5
$b = 10
$c = 2
$result = ($a + $b) * $c - ($b / $c)
Write-Output $result

You can see the output in the screenshot below:

Arithmetic Operators in PowerShell

Read PowerShell Not Operator

PowerShell Arithmetic Operators Examples

Now, let me show you some examples of PowerShell arithmetic operators.

Example-1: Calculate Dates

PowerShell can perform arithmetic operations on dates using the Get-Date cmdlet and New-TimeSpan cmdlet.

Here is an example where we are adding 10 days to today’s date.

$today = Get-Date
$futureDate = $today + (New-TimeSpan -Days 10)
Write-Output $futureDate  # Output: Date 10 days from today

After I executed the above script using VS code, you can see the output in the screenshot below:

Arithmetic Operators in PowerShell Examples

Example-2: Unit Conversions

PowerShell can be used to perform unit conversions, such as converting inches to centimeters. Here is the complete script.

$inches = 12
$centimeters = $inches * 2.54
Write-Output $centimeters  # Output: 30.48

Example-3: Financial Calculations

You can use PowerShell to perform financial calculations, such as calculating compound interest. Here is the complete PowerShell script.

$principal = 1000
$rate = 5 / 100
$time = 10
$amount = $principal * (1 + $rate) ** $time
Write-Output $amount  # Output: 1628.89

Conclusion

In PowerShell, arithmetic operators are used for mathematical calculations. These operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). In this tutorial, I have explained how to use PowerShell arithmetic operators with examples.

Addition (+):

  • Adds two numbers.
  • Can also concatenate strings and arrays.

Subtraction (-):

  • Subtracts one number from another.
  • Can be used to negate a number.

Multiplication (*):

  • Multiplies two numbers.
  • Can repeat strings and arrays a specified number of times.

Division (/):

  • Divides one number by another.
  • Returns the quotient.

Modulus (%):

  • Returns the remainder of a division operation.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.