Comparing Versions

by Sep 1, 2010

When you compare version strings, PowerShell will use alphanumeric algorithms, which may lead to confusing results:

'3.4.22.12' -gt '22.1.4.34'
True

You should convert the strings to a System.Version type to compare version strings right. In fact, converting the left hand value is enough because PowerShell automatically converts the right hand value to the same type:

[System.Version] '3.4.22.12' -gt '22.1.4.34'
False

Twitter This Tip! ReTweet this Tip!