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
1 2 3 4 5 6 7 |
$Data = '4.3.0.3' [System.Version]::Parse($Data) Major Minor Build Revision ----- ----- ----- -------- 4 3 0 3 |
For my SQLinstances I used the following
1 2 3 4 5 6 7 8 9 |
$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
1234567 PS C:\Windows\system32> $OutputEdition Patchlevel Version Instance------- ---------- ------- --------Standard Edition 11.2.5343.0 11.2.5058.0 MSSQL11.MSSQLSERVERStandard Edition 11.1.3156.0 11.1.3000.0 MSSQL11.SYSTEMDATAExpress Edition 11.2.5343.0 11.2.5343.0 MSSQL11E.LOCALDB
When you select only the patchlevel in resulted in
1234 PS C:\Windows\system32> $Output.patchlevel11.2.5343.011.1.3156.011.2.5343.0
And the Version resulted in
1234567 PS C:\Windows\system32> $Output.versionMajor Minor Build Revision----- ----- ----- --------11 2 5058 011 1 3000 011 2 5343 0
Now I have data in a table where I can compare versions.
Hope this was informative for you.
rgds
wvsandwijk
I am a Technical Specialist - Cloud Infrastructures from the Netherlands.
My Skills are mainly PowerShell, VMWare, Hyper-V, Microsoft System Center products and Windows Azure Pack.
I work for a company in Utrecht The Netherlands.
My Skills are mainly PowerShell, VMWare, Hyper-V, Microsoft System Center products and Windows Azure Pack.
I work for a company in Utrecht The Netherlands.
Latest posts by wvsandwijk (see all)
- [PS] VMware ESX Certificate validation - September 7, 2018
- PoSH-Git : unable to start ssh-agent service, error :1058 - July 22, 2018
- [PS] Speedup VMWare PowerShell module - June 11, 2018