Win10 vs Win 8.1 os version

Hi,
Any idea why this is happening ?
On a windows 10

PS C:\> $os = Get-WmiObject win32_operatingsystem
PS C:\> $o.version -gt 6.2
False

While in Win 8.1

PS C:\> $os = Get-WmiObject win32_operatingsystem
PS C:\> $o.version -gt 6.2
True

See the documentation at https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx.

Version is a string; you can’t do numeric comparisons with it, unless you cast it as a [single] first. And even that will fail if the version is ever something like 6.2.2, since that’s not actually a number.

Wouldn’t it work when you cast it to [VERSION]?

You’d have to cast both values - the Version property and the 6.2 comparison - to [version], but yes, that’s what [version] is for.

Cool … thanks

The casting that is being referred to would be something like this:

[version]$os.Version -gt [version]"6.2"

Powershell will do type conversions for you in some cases, however, “strong-typed” code will not leave things to chance. You want to do comparisons to the same type string to string, int to int, etc. and in this case version to version. Casting attempts to convert the value to the type so that a proper comparison can be done. Even when I was playing with the version cast, it didn’t like X.0:

PS C:\Users\Rob> [version]10.0
Cannot convert value "10" to type "System.Version". Error: "Version string portion was too short or too long."
At line:1 char:1
+ [version]10.0
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocation
 

PS C:\Users\Rob> [version]"10.0"

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      -1     -1      

It’s especially important when doing comparisons like -gt, because you are basically doing something like TEN -gt 6.2.

exactly what i was looking for , the [version] cast , didn’t know about it before
nice find :slight_smile:

PS C:\> [version]$os.version -gt [version]"6.2"
True