Building on the great comments on the question:
tl;dr
Replace $val.'agent version' -eq 7.8.5.005
with:
# Note the [version] cast, and the '...' around the RHS operand.
# Because the LHS uses [version], it isn't also needed before '7.8.5.5'
[version] $val.'agent version' -eq '7.8.5.5' # Note: 0-padding is irrelevant
In general, to construct a literal [version]
instance, you must cast from a string:
[version] '7.8.5.5' # '...' required
To address the general question implied by your post's title:
As for what you tried:
The Windows registry has no data type that directly represents a version number - a string value (REG_SZ
) is used to represent version numbers, and that translates into a .NET [string]
instance via Get-ItemProperty
or - to retrieve a registry value directly - via Get-ItemPropertyValue
.
Similarly, PowerShell also has no literal notation for version numbers, but you can cast a string to [version]
in order to construct a System.Version
instance.
As zett42 notes, the unquoted 7.8.5.005
literal effectively evaluates to $null
, because it is parsed as [double]
instance 7.8
on which you're trying to access the (implicitly stringified) [double]
instance 5.005
as a property, which obviously doesn't exist.
You can gain insights into how PowerShell parses such expressions by enclosing them in a script block ({ ... }
), and drilling down into that script block's .Ast
property - note how 5.005
is listed as a Member
access:
PS> { 7.8.5.005 }.Ast.EndBlock.Statements.PipelineElements.Expression
Expression : 7.8
Member : 5.005
Static : False
NullConditional : False
StaticType : System.Object
Extent : 7.8.5.005
Parent : 7.8.5.005
Only [version]
instances compare meaningfully as version numbers.
Since it is sufficient for the LHS of a comparison operation, such as with -eq
, to be of type [version]
in order to compare version numbers, you can take a shortcut and use a string literal as the RHS, as shown at the top.