PowerShell: How to Increment a Version Number


Often you may want to increment a version number in PowerShell.

You can use the following syntax to do so:

$split_version = $my_version.split(".")
$split_version[3] = [int]$split_version[3] + 1
$my_version = $split_version -join "."

This particular example increments the last value in the version number stored in the string named $my_version.

Here is how this syntax works:

  • First, we use the split command to split the version number into elements based on where any period occurs.
  • Next, we add 1 to the element in index position 3 of the resulting split.
  • Lastly, we use the -join operator to join back all of the individual elements into one string, using a period as a separator.

The result is that the original version number is incremented by 1.

For example, a version number of 1.1.0.3 would become 1.1.0.4.

The following example shows how to use this syntax in practice.

Example: How to Increment a Version Number in PowerShell

Suppose that we have a string named $my_version with the following version number stored in it:

  • 1.1.0.3

Suppose that we would like to increment the version number by 1 to obtain the following new version number:

  • 1.1.0.4

We can use the following syntax to do so:

$my_version = "1.1.0.3"

$split_version = $my_version.split(".")
$split_version[3] = [int]$split_version[3] + 1
$my_version = $split_version -join "."

The following screenshot shows how to use this syntax in practice:

PowerShell increment version number

Notice that when we view the $my_version variable again we can see that it has been incremented to 1.1.0.4.

Note that version numbers are stored in formats where the Major, Minor, Build and Revision numbers are separated by periods.

In this particular example we used $split_version[3] to increment the Revision portion of the version number by 1.

However, you could use similar syntax such as $split_version[2] to instead increment the Build portion of the version number by 1.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Sort Version Strings
PowerShell: How to Sort a Table with Format-Table
PowerShell: How to Sort by Specific Column
PowerShell: How to Use Sort-Object with Multiple Properties

Leave a Reply