When writing PowerShell scripts, you’ll often need to increment variables to keep track of counts, generate sequences, or perform loops. PowerShell provides a few different ways to increment variables. In this tutorial, I will explain how to increment variables in PowerShell using different methods and examples.
To increment a variable in PowerShell, you can use the increment (++) operator. For example, if you have a variable $count and want to increase its value by 1, you can simply write $count++. This operator can be used either before (pre-increment) or after (post-increment) the variable, depending on when you want the increment to take effect.
PowerShell increment variable
Now, let me show you how to increment a variable in PowerShell using different methods.
1. Using the Increment (++) Operator
PowerShell supports the ++ increment operator to increase the value of a variable by 1. You can use the operator either before (pre-increment) or after (post-increment) the variable.
For example, let’s say you want to count the number of files in a directory that match a certain pattern:
$count = 0
Get-ChildItem -Path "C:\MyFolder" -Filter "*.txt" | ForEach-Object {
$count++
}
Write-Host "Found $count text files in the directory."In this script, we initialize $count to 0 and then use $count++ to increment it by 1 for each matching text file found.
I executed the above PowerShell script, and you can see the output in the screenshot below:

Check out Check if a Variable is Null in PowerShell
2. Using the += Operator
Another way to increment a variable in PowerShell is by using the += operator to add a value to the existing variable value. This is useful when you want to increment by a value other than 1.
For instance, let’s generate a sequence of even numbers:
$num = 0
for ($i = 1; $i -le 5; $i++) {
$num += 2
Write-Host $num
}This will output:
2
4
6
8
10You can see the output in the screenshot below, I executed the PowerShell script using VS code.

3. Increment Variable in Function
You can also increment variables inside PowerShell functions. Just make sure to use the $script: scope modifier if you want the incremented value to persist outside the function.
Here’s an example that increments a global counter each time the function is called:
$global:callCount = 0
function Do-Something {
$script:callCount++
Write-Host "Function called $callCount times"
}
Do-Something
Do-Something
Do-SomethingOutput:
Function called 1 times
Function called 2 times
Function called 3 timesHere is the output in the screenshot below:

4. Increment Variables in Loops
Loops are a common scenario where you might need to increment a variable. PowerShell supports several types of loops, including for, foreach, and while.
PowerShell increment variable in for loop
Here is an example of a PowerShell for loop where I have shown how to increment.
# Initialize the variable
for ($i = 0; $i -lt 5; $i++) {
Write-Output "Iteration: $i"
}This script uses a for loop to iterate five times, incrementing the variable $i on each iteration. The output will be:
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4Here is the exact output in the screenshot below:

PowerShell increment variable in while loop
Now, let me show you how to increment a variable in a while loop in PowerShell.
In this example, we’ll create a simple script that increments a counter variable until it reaches a specified limit.
# Initialize the variable
$counter = 0
# Define the limit
$limit = 10
# While loop to increment the counter
while ($counter -lt $limit) {
Write-Output "Counter: $counter"
$counter++
}
Write-Output "Final Counter Value: $counter"Explanation:
- Initialization: The variable
$counteris initialized to 0, and$limitis set to 10. - Condition: The
whileloop continues to execute as long as$counteris less than$limit. - Increment: Inside the loop, the current value of
$counteris printed, and then$counteris incremented by 1 using the++operator. - Termination: When
$counterreaches 10, the loop terminates, and the final value of$counteris printed.
Output:
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Counter: 7
Counter: 8
Counter: 9
Final Counter Value: 10This example explains how to use a while loop to increment a variable until a certain condition is met. The ++ operator is used to increment the $counter variable on each iteration of the loop.
You can also see the exact output in the screenshot below.

PowerShell increment counter in foreach loop
Let me show you how to increment a variable in a foreach loop in PowerShell.
In this example, we’ll iterate over an array of items and increment a counter variable for each item.
# Initialize the array
$array = @('Apple', 'Banana', 'Cherry', 'Date', 'Elderberry')
# Initialize the counter variable
$counter = 0
# Foreach loop to iterate over the array and increment the counter
foreach ($item in $array) {
Write-Output "Processing: $item"
$counter++
}
Write-Output "Total items processed: $counter"Explanation:
- Initialization: An array
$arrayis initialized with a list of fruit names. The counter variable$counteris initialized to 0. - Foreach Loop: The
foreachloop iterates over each item in the array. For each iteration, it prints the current item being processed and increments the$countervariable by 1 using the++operator. - Output: After the loop completes, the total number of items processed is printed.
Output:
Processing: Apple
Processing: Banana
Processing: Cherry
Processing: Date
Processing: Elderberry
Total items processed: 5This example explains how to use a foreach loop to iterate over an array and increment a counter variable. The ++ operator is used to increment the $counter variable on each iteration of the loop.
5. Increment Version Numbers
I added this example because it is a very common real-world example. Suppose you want to increment version numbers in PowerShell. Let’s say you have a version stored as a string like “1.0.4,” and you want to increment the last part for each script run.
$version = "1.0.4"
$parts = $version.Split(".")
$parts[$parts.Length-1] = ([int]$parts[$parts.Length-1] + 1).ToString("00")
$newVersion = $parts -join "."
Write-Host "New version: $newVersion"This splits the version string into an array, increments the last part, and rejoins it into the new version string “1.0.5”.
PowerShell Increment Variable by 1
In PowerShell, you can use the unary increment operator ++ to increment a variable by 1. This operator increases the value of a variable by 1.
The unary increment operator ++ is a concise way to increase the value of a variable by 1. It can be used in both pre-increment and post-increment forms
But let me explain this with another real-time example.
Let’s consider a scenario where you need to keep track of the number of user logins in a system. Each time a user logs in, you increment the counter by 1.
# Initialize the login counter
$loginCounter = 0
# Simulate user logins
$userLogins = @('User1', 'User2', 'User3', 'User4', 'User5')
# Increment the counter for each user login
foreach ($user in $userLogins) {
Write-Output "User logged in: $user"
$loginCounter++
}
# Output the total number of logins
Write-Output "Total number of logins: $loginCounter"Explanation:
- Initialization: The variable
$loginCounteris initialized to 0 to keep track of the number of logins. - User Logins: An array
$userLoginsis initialized with a list of usernames representing user logins. - Foreach Loop: The
foreachloop iterates over each user in the$userLoginsarray. For each iteration, it prints the username of the user who logged in and increments the$loginCountervariable by 1 using the++operator. - Output: After the loop completes, the total number of logins is printed.
Output:
User logged in: User1
User logged in: User2
User logged in: User3
User logged in: User4
User logged in: User5
Total number of logins: 5This example explains how to use the unary increment operator ++ to increment a variable by 1 in a PowerShell script. You can even see the exact output in the screenshot below:

Conclusion
I hope these examples help you understand how to increment variables in PowerShell. Here, I explained the examples below:
- Increment variable by 1 in PowerShell
- Increment variable in function in PowerShell
- Increment variable in loops in PowerShell
- Increment variable in for loop in PowerShell
- Increment variable in while loop in PowerShell
- Increment counter in foreach loop in PowerShell
In the comment section below, let me know if you have any other questions, and I will reply as soon as possible.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.