I've got a problem with a comparison between two Versions.
PS C:\Windows\system32> $soft.DisplayVersion -lt "124.0"
False
I expect it to return True because the value of $soft.DisplayVersion is :
PS C:\Windows\system32> $soft.DisplayVersion
95.0
Object Type :
PS C:\Windows\system32> $soft.DisplayVersion.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String
I also tried this:
PS C:\Windows\system32> $soft.DisplayVersion -lt 124.0
False
I don't know if it helps but $soft.DisplayVersion comes from this:
Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object DisplayName -match "Mozilla Firefox"
Let me know if you need extra information to understand my problem.
The registry key of origin and the comparison operand imply that you're dealing with version numbers.
To meaningfully compare version numbers based on their string representations, cast them to
[version]:Note:
For brevity, you could omit the RHS
[version]cast, because the LHS cast would implicitly coerce the RHS to[version]too. However, using it on both sides can help conceptual clarity.For a
[version]cast to succeed, the input string must have at least 2 components (e.g.[version] '124.0'and at most 4 (e.g,[version] '124.0.1.2'), and each component must be a positive decimal integer (including0).[version]is not capable of parsing semantic version numbers, however, which are limited to 3 components and may contain non-numeric suffixes (e.g.,7.5.0-preview.2).Use
[semver]to parse semantic versions; however, that type is available in PowerShell (Core) 7 only.Unlike with
[version], a single component is sufficient in the input string of a[semver]cast; e.g.,[semver] '124'works to create version124.0.0.As for what you tried:
$soft.DisplayVersion -lt "124.0"and$soft.DisplayVersion -lt 124.0are equivalent, and neither works as intended:-ltcoerces the RHS operand to a string too, and then performs lexical comparison, which is not what you want:'95.0' -lt '124.0''95.0' -lt 124.0are both false, because the character'9'is greater than the character'1'.[decimal]$soft.DisplayVersion -lt 124.0(from your own answer) works only for version-number strings up to two components; e.g.[decimal] '95.0.1'would break.