Comparing versions in PowerShell


Was recently asked on twitter how to compare properties of an object in the version format  (ie 1.2.3.4 >= 3.2.1.1). Well, as the property was string, which came into my mind was to remove the point. Something like

(Get-WmiObject win32_pnpsigneddriver |
Where-Object {(($_.DriverVersion).replace(‘.’,”)) -ge ‘3211’}).DriverVersion

Well, this solution will fail in some cases , as if you have NULL or other chars in the property. So, doing a better research, I found a post from Shay Levy (Actually he helped someone else)

https://www.cupfighter.net/2009/06/powershell-comparing-version-numbers

You can use the [version] type in Powershell to perform the operation Alegre (as showing in the post above)

(Get-WmiObject win32_pnpsigneddriver |
Where-Object {[version]$_.DriverVersion -ge [version]’1.0.20′}).DriverVersion

Awesome !!!

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Powershell. Bookmark the permalink.

Leave a comment