1

I've found a bug with a software version checking script I created, and I've narrowed it down to PowerShell's version comparison behaviour illustrated below.

> $PSVersionTable

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
PSVersion                      5.1.19041.2673                                                                                                                                                                                                                    
PSEdition                      Desktop                                                                                                                                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                           
BuildVersion                   10.0.19041.2673                                                                                                                                                                                                                   
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                   
WSManStackVersion              3.0                                                                                                                                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                               
SerializationVersion           1.1.0.1

>([version]113.0.0 -ge [version]114.0.0)
True

> ([version]113.0.0 -gt [version]114.0.0)
False

> (113 -ge 114)
False

> (113 -gt 114)
False

Surely the result of the first comparison:

>([version]113.0.0 -ge [version]114.0.0)
True

…should be False, the same as when using the -gt operator? What am I missing?

  • 1
    Use: `([version]"113.0.0" -ge [version]"114.0.0")`. This can be seen/checked by the difference between: `[version]113.0.0` and `[version]"113.0.0"`. – Luuk Apr 15 '23 at 09:37
  • earlier answer to the same question: https://stackoverflow.com/a/48424840/724039 – Luuk Apr 15 '23 at 09:46
  • You must use _quoting_, i.e. cast a _string_ (e.g, `'1.2.3.4'`) to `[version]` in order to construct a version-number object as intended, e.g. `[version] '1.2.3.4'` An _unquoted_ expression such as `1.2.3.4` quietly evaluates to `$null` in PowerShell, because `3.4` is interpreted as an attempt to access (non-existent) nested properties `3` and `4` on `[double]` literal `1.2`. Therefore, `[version] 1.2.3.4` is the same as `[version] $null`, which is `$null` – mklement0 Apr 15 '23 at 10:37
  • Thank you to all for your generosity of responses. So it turns out my version checking is fine, and the problem lies elsewhere! – OneOfTheDamons Apr 16 '23 at 10:02

0 Answers0