Compare Versions

by Mar 14, 2016

Ever wanted to compare software versions? If you do it like this, the result is off:

 
PS> '3.12.11.100' -gt '11.1.22.91'
True
 

Here, PowerShell does a string comparison. To get the correct result for version numbers, simply hint the type:

 
PS> [Version]'3.12.11.100' -gt '11.1.22.91'
False 
 

The type needs to be defined for the left side only. PowerShell automatically converts the right side to the type of the left side if they are different.

Twitter This Tip! ReTweet this Tip!