Comparing version numbers – Part II

Today I’d like to share a function for comparing semantic version numbers in PowerShell. To recap: Software developers should use a logical versioning style to better manage dependencies and to just make their releases and cycles well understandable. There is some kind of a specification out there. I’m not sure how “official” this is but it doesn’t look bad and sounds reasonable. Of course, if the 4th element is used for “beta” or “rc” like they suggest in the specification, the comparison solely by numbers is impossible. But you get the picture.

You might have seen the first article about a quick fix if you compare versions with more than 4 elements. Here’s a function that will shorten a version string to just 4 elements but will not fail if the version number has less than 4 elements.

I’m sure by using RegEx you could solve that problem much more sleek but for now I’ll just stick with the following solution:

Let’s check out how we use that function:

Enclose the expression with parentheses to use it directly in your code:

Output:

So, if you’re a RegEx geek, don’t hesitate to post your solution in the comments 😉

If you’re struggling with the comparison of file versions, check out this askpfe article to learn how to do proper file comparison.

Leave a Comment