Incrementing and Decrementing in PowerShell

Marion Paul Kenneth Mendoza Jan 30, 2023 Mar 07, 2022
  1. Incrementing and Decrementing in PowerShell
  2. Pre-Incrementing and Post Incrementing/Decrementing in PowerShell
  3. Pre-Incrementing and Post Incrementing/Decrementing With for Loops in PowerShell
  4. Pre-Incrementing and Post Incrementing/Decrementing With Do-While Loops in PowerShell
Incrementing and Decrementing in PowerShell

This article will discuss Incrementing and Decrementing and when to use Pre-Incrementing and Post-Incrementing (or Decrementing) in PowerShell.

Incrementing and Decrementing in PowerShell

Like any programming language, the Increment operator signified by a double plus sign (++) will increase the variable’s value by 1. In contrast, the Decrement operator represented by a double minus sign (--) will decrease by 1.

Incrementing and Decrementing operators are easier to write when compared to the traditional way of writing it. Technically $num++ is more seamless when compared to $num = $num + 1.

Example Code:

$num = 5
$num++ # 6
$num++ # 7
$num++ # 8

$number = 5
$number-- # 4
$number-- # 3
$number-- # 2

Incrementing and Decrementing are often used in loops as exit conditions. First, however, we must adequately understand when to increment/decrement in a loop.

Pre-Incrementing and Post Incrementing/Decrementing in PowerShell

We can write the increment/decrement operators before the variable in PowerShell.

Syntax:

++$number
--$number

By definition, the pre-increment/decrement operators increment/decrement their operand by one, and the value of the expression returns the resulting incremented (or decremented) value.

Pre-Incrementing and Post Incrementing/Decrementing With for Loops in PowerShell

The post-increment operators are frequently seen in for loops. For example, the value for variable $i is used as an index indicator in a loop and then incremented by one after a single pass.

The increment happens in the for loop after all statements inside the loop block are executed.

Example Script:

for ($i = 0; $i -le 15; $i++) {
    Write-Host $i
}

Output:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Since the for loop will consistently execute the increment/decrement at the end of every pass, the increment/decrement operator placements will not matter too much.

Pre-Incrementing and Post Incrementing/Decrementing With Do-While Loops in PowerShell

The Do-While loop will execute the loop first before checking the exit condition. For example, try running the script below that numbers 1 through 5 using the Do-While loop.

Example Script:

$num = 1
Do{
    $num
} While ($num++ -le 5)

Output:

1
2
3
4
5
6

Since we are post-incrementing, the loop will first check the condition, and since $num is still equal to 5, the loop will allow the script to run another pass. After the condition has been checked, $num will increment to 6, thus the returned output.

We want to pre-increment the $num value in the following example. In this way, when the condition is evaluated, the $num value will be less than or equal 5.

All we need to do is move where ++ appears to pre-increment to stop the loop when the $num value is equal to 5.

Example Script:

$num = 1

Do
{
    $num
} While (++$num -le 5)

Output:

1
2
3
4
5

If we use the While loop instead, post-incrementing/decrementing will work fine since the condition is checked first before running the loop.

The point is that incrementing and decrementing are as important as where we place these operators in programming logic, so we should use them carefully when scripting.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn