locked
Incrementing Version Numbers in PowerShell RRS feed

  • Question

  • Hi All

    Not a question but I had a situation where I needed to organise version numbers and then pull the latest and increment it. I thought this might be of help to others who need to do the same thing.

    I needed to get my version number from git. This is how you do it.

    ((git tag).split([environment]::newline) | sort {[version] ($_)} -descending)[0]

    The above will work for any multi-line string of version numbers. PowerShell is able to sort them when you call the [Version] accelerator

    I created a function to increment the version number, it's used as follows:

    PS C:\bitbucket\pcs_cam> ((git tag).split([environment]::newline) | sort {[version] ($_)} -descending)[0]                              0.0.21
    PS C:\bitbucket\pcs_cam> ((git tag).split([environment]::newline) | sort {[version] ($_)} -descending)[0] | Increment-Version -build                 

    0.0.22
    PS C:\bitbucket\pcs_cam> ((git tag).split([environment]::newline) | sort {[version] ($_)} -descending)[0] | Increment-Version -minor                 

    0.1.0
    PS C:\bitbucket\pcs_cam> ((git tag).split([environment]::newline) | sort {[version] ($_)} -descending)[0] | Increment-Version -major                 

    1.0.0

    Here is the function:

    Function Increment-Version {
    <#
    .SYNOPSIS
    Increments a version number.
    .DESCRIPTION
    Increments a Major, Minor or Build version number.
    .PARAMETER Version
    Version number to increment.
    .PARAMETER Major
    Increments the major version
    .PARAMETER Minor
    Increments the minor version
    .PARAMETER Build
    Increments the build version
    .EXAMPLE
    Increments the build version
        
    Increment-Version -Version 0.0.10 -Build
    
    0.0.11
    .EXAMPLE
    Increments the minor version
        
    Increment-Version -Version 0.1.10 -Minor
    
    0.2.0
    .EXAMPLE
    Increments the major version
        
    Increment-Version -Version 1.1.10 -Major
    
    2.0.0
    .NOTES
    #>    
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName, ValueFromPipeline)][ValidateNotNullOrEmpty()][Version]$Version,
            [Parameter(ParameterSetName = 'Build', Mandatory = $true)][Switch]$Build,
            [Parameter(ParameterSetName = 'Minor', Mandatory = $true)][Switch]$Minor,
            [Parameter(ParameterSetName = 'Major', Mandatory = $true)][Switch]$Major
        )
    
        Switch ($PSCmdlet.ParameterSetName) {
            Build {Return "{0}.{1}.{2}" -f $Version.Major, $Version.Minor, ($Version.Build +1)}
            Minor {Return "{0}.{1}.{2}" -f $Version.Major, ($Version.Minor +1), 0}
            Major {Return "{0}.{1}.{2}" -f ($Version.Major +1), 0, 0}
        }
    }

    Hope this helps.


    • Edited by cammmy Friday, September 27, 2019 1:51 PM
    Friday, September 27, 2019 1:50 PM

Answers

  • Unfortunately the code is unnecessary because we can just increment any individual field in one line of code.

    $v = [version]'1.1.1.1'
    [version]::New($v.Major,$v.Minor,$v.Build,$v.Revision+1)

    Much cleaner because we use PowerShell and the Net object methods to perform our increment.  Yu can choose to increment any field by just adding one to the field value.

    Note that a version number has four fields not three.

    With PowerShell it is best to avoid strings when we have objects.


    \_(ツ)_/



    • Edited by jrv Friday, September 27, 2019 2:07 PM
    • Proposed as answer by Stoyan ChalakovMVP Friday, September 27, 2019 2:24 PM
    • Marked as answer by jrv Thursday, March 19, 2020 2:46 AM
    Friday, September 27, 2019 2:04 PM

All replies

  • Unfortunately the code is unnecessary because we can just increment any individual field in one line of code.

    $v = [version]'1.1.1.1'
    [version]::New($v.Major,$v.Minor,$v.Build,$v.Revision+1)

    Much cleaner because we use PowerShell and the Net object methods to perform our increment.  Yu can choose to increment any field by just adding one to the field value.

    Note that a version number has four fields not three.

    With PowerShell it is best to avoid strings when we have objects.


    \_(ツ)_/



    • Edited by jrv Friday, September 27, 2019 2:07 PM
    • Proposed as answer by Stoyan ChalakovMVP Friday, September 27, 2019 2:24 PM
    • Marked as answer by jrv Thursday, March 19, 2020 2:46 AM
    Friday, September 27, 2019 2:04 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, October 3, 2019 1:53 AM
  • I wrote a function for it as I wanted to roll over the versions to the next values if they exceeded a certain value per segment. It handles 3 or 4 segments, and will also allow for version suffixes such as -alpha.

    Clear-Host
    
    function Increment-Version([string]$version) {
        $vstr_regex = "^(?<version>\d+\.\d+\.\d+|\d+\.\d+\.\d+.\d+)(?<suffix>-.*)?$"
    
        $match = $version -match $vstr_regex
        if (!$match) {
            throw "invalid version format!"
        }
        $suffix = $matches.suffix
    
        $versionParts = $matches.version.Split('.') 
        [array]::Reverse($versionParts)
        $incrementNext = $false;
        $limit = 0;
    
        #increment the version, if the number in each octet exceeds the nines: 1.99.999.9999 then increment the preceeding number
        for ($i = 0; $i -lt $versionParts.Length; $i++)
        {
            $limit = [int]("9" * ($versionParts.Length - $i));
            $val = ([int]$versionParts[$i]) + 1;
    
    #        //if this is the Major value, then increment it if needed, and bail so we dont accidentally reset it to zero
            if ($i -eq $versionParts.Length - 1)
            {
                if ($incrementNext) { $versionParts[$i] = $val; }
                continue;
            }
    
            if ($incrementNext -or $i -eq 0)
            {
                if($val -gt $limit) {
                    $incrementNext = $true;
                    $versionParts[$i] = 0 
                } else {
                    $versionParts[$i] = $val;
                    $incrementNext = $false;
                }
            }
            else
            {
                $incrementNext = $false;
            }
        }
        [array]::Reverse($versionParts)
        $newVersion = "$([string]::Join(".", $versionParts))$suffix";
        return $newVersion;
    }
    
    Increment-Version -version "1.22.33-beta"
    
    Increment-Version -version "1.22.33.44-alpha"
    
    Increment-Version -version "1.22.998.9999"
    
    Increment-Version -version "1.22.999.9999"
    
    
    
    



    //Will write code for food

    Wednesday, March 18, 2020 9:49 PM
  • Good to know. I did a fair bit of searching before writing that (I try not to re-invent the wheel if I don't have to) but didn't see anything like that come up.

    As for the three fields, that's what was adopted by the company.

    Tuesday, March 24, 2020 3:53 PM