July 11, 2025

USEFUL-IT

A blog for USEFUL-IT information

[PS] Convert string to version

How to convert a string to version (Major, Minor, Build, Revision) in PS

Sometimes you struggle in getting the version of a piece of software or file that is installed on my windows boxes in the right format.

Well I struggled with comparing installed versions of my installed SQL instances.
.Net and PowerShell helped me resolve this issues.

Use the following .net command

$Data = '4.3.0.3'
[System.Version]::Parse($Data)


Major  Minor  Build  Revision
-----  -----  -----  --------
4      3      0      3

For my SQLinstances I used the following

$SQLInstances = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL*').PSChildName
$Output = ForEach ($Instance in $SQLInstances) {
            $Data = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$Instance\Setup" -ErrorAction SilentlyContinue
        
            $Data | Select Edition,
                           @{Name = 'Patchlevel' ; Expression = { $_.Patchlevel }} ,
                           @{Name = 'Version'    ; Expression = { [System.Version]::Parse($_.Version) }} ,
                           @{Name = 'Instance'   ; Expression = { $Instance }}
}

This resulted in

PS C:\Windows\system32> $Output

Edition          Patchlevel  Version     Instance           
-------          ----------  -------     --------           
Standard Edition 11.2.5343.0 11.2.5058.0 MSSQL11.MSSQLSERVER
Standard Edition 11.1.3156.0 11.1.3000.0 MSSQL11.SYSTEMDATA 
Express Edition  11.2.5343.0 11.2.5343.0 MSSQL11E.LOCALDB

When you select only the patchlevel in resulted in

PS C:\Windows\system32> $Output.patchlevel
11.2.5343.0
11.1.3156.0
11.2.5343.0

And the Version resulted in

PS C:\Windows\system32> $Output.version

Major  Minor  Build  Revision
-----  -----  -----  --------
11     2      5058   0       
11     1      3000   0       
11     2      5343   0

Now I have data in a table where I can compare versions.

Hope this was informative for you.

rgds

 

About The Author