Powershell Increment value by 0.0.1 Powershell Increment value by 0.0.1 powershell powershell

Powershell Increment value by 0.0.1


Convert to a [version] object:

# read existing version$version = [version](Get-Content 'package.json' | ConvertFrom-Json).version# create new version based on previous with Build+1$bumpedVersion = [version]::new($version.Major, $version.Minor, $Version.Build + 1)

Alternatively, split the string manually:

$major,$minor,$build = $version.Split('.')# increment build number$build = 1 + $build# stitch back together$bumpedVersion = $major,$minor,$build -join '.'


To complement Mathias' helpful answer with a concise alternative based on the -replace operator:

# PowerShell [Core] only (v6.2+) - see bottom for Windows PowerShell solution.PS> '0.0.3', '1.123.3' -replace '(?<=\.)[^.]+$', { 1 + $_.Value }0.0.41.123.4
  • Regex (?<=\.)[^.]+$ matches the last component of the version number (without including the preceding . in the match).

  • Script block { 1 + $_.Value } replaces that component with its value incremented by 1.

For solutions to incrementing any of the version-number components, including proper handling of [semver] version numbers, see this answer.


In Windows PowerShell, where the script-block-based -replace syntax isn't supported, the solution is more cumbersome, because it requires direct use of the .NET System.Text.RegularExpressions.Regex type:

PS> '0.0.3', '1.123.3' | foreach {        [regex]::Replace($_, '(?<=\.)[^.]+$', { param($m) 1 + $m.Value })    }0.0.41.123.4


C:\> $v = "1.2.3"C:\> $(($v -split "\.")[0..1] + "$([int](($v -split '\.') |Select-Object -Index 2) +1)") -join "."