How to Increment a String Variable by 1 in PowerShell?

Recently, I got a requirement from one of my clients to increment a string variable that represents a version number in PowerShell. This is a little tricky, but there is a way to do it. In this tutorial, I will show you how to increment a string variable by 1 in PowerShell.

To increment a string variable with a numeric suffix in PowerShell, you can split the string into parts, convert the numeric portion to an integer, increment it, and then reassemble the string. For example, given a version number “1.0.0”, you can use the following script:

$version = "1.0.0"
$parts = $version.Split('.')
$parts[2] = [int]$parts[2] + 1
$newVersion = "$($parts[0]).$($parts[1]).$($parts[2])"
Write-Output $newVersion  # Outputs "1.0.1"

This method ensures the numeric portion is correctly incremented while preserving the original format.

Now, let me show you how to increment a string variable in PowerShell.

Method 1: Numeric Suffix Increment

The simplest form of string incrementation involves a numeric suffix. This is particularly useful for creating unique identifiers or version numbers.

Example: Incrementing a Version Number

Let’s say you have a version number stored as a string, such as “1.0.0”. You can increment the last part of the version number using the following script:

$version = "1.0.0"
$parts = $version.Split('.')
$parts[2] = [int]$parts[2] + 1
$newVersion = "$($parts[0]).$($parts[1]).$($parts[2])"
Write-Output $newVersion  # Outputs "1.0.1"

In this example, we split the version number into an array, increment the last element, and then reassemble the string.

I executed the above PowerShell script, and you can see the output in the screenshot below:

powershell increment string variable by 1

Check out PowerShell Print Variable

Method 2: Alphabetic Increment

Incrementing alphabetic characters in PowerShell is more complex than numeric increments. This method is useful when you need to cycle through letters, such as in column naming (A, B, C, …, Z, AA, AB, etc.).

Example: Incrementing Column Names

Here’s a PowerShell script that increments alphabetic characters:

function Increment-Alpha {
    param (
        [string]$input
    )
    $charArray = $input.ToCharArray()
    for ($i = $charArray.Length - 1; $i -ge 0; $i--) {
        if ($charArray[$i] -ne 'Z') {
            $charArray[$i] = [char]([int][char]$charArray[$i] + 1)
            return -join $charArray
        } else {
            $charArray[$i] = 'A'
            if ($i -eq 0) {
                return 'A' + (-join $charArray)
            }
        }
    }
}

$column = "Z"
$newColumn = Increment-Alpha $column
Write-Output $newColumn  # Outputs "AA"

This function increments a string of alphabetic characters, handling the carry-over when a ‘Z’ is encountered.

Method 3: Custom String Patterns

For more complex scenarios, you might need to increment a string that contains both letters and numbers, or follows a custom pattern.

Example: Incrementing a Custom Identifier

Consider a scenario where you have an identifier like “ID-001” that you need to increment:

function Increment-Identifier {
    param (
        [string]$identifier
    )
    if ($identifier -match '^(.*?)(\d+)$') {
        $prefix = $matches[1]
        $number = [int]$matches[2] + 1
        return "$prefix$($number.ToString('D3'))"
    } else {
        throw "Identifier format not recognized"
    }
}

$id = "ID-001"
$newId = Increment-Identifier $id
Write-Output $newId  # Outputs "ID-002"

This function uses a regular expression to separate the prefix from the numeric part, increments the number and then reassembles the string.

You can see the exact output in the screenshot below:

increment string variable by 1 in PowerShell

In this tutorial, I explained how to increment string variable by 1 in PowerShell with a few examples.

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.