I am trying to ascertain whether 2 application version strings are equal in PowerShell and am getting a confusing result:
PS C:\> $version = wmic product where "caption like '%Citrix Workspace Inside%'" get Version
PS C:\> $versionString = "22.5.0.4"
PS C:\> $version[2]
22.5.0.4
PS C:\> $versionString
22.5.0.4
PS C:\> $version[2].GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\> $versionString.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\> $version[2] -eq $versionString
False
Is it something to do with what's returned from the 'wmic' command? The return type of $version is Object[], an object array so I've just indexed it to grab the string I need. Both $version[2] and $versionString have the same value and type but return False when compared for equality. Any light on this would be greatly appreciated.
Thanks
First, a warning:
The
Win32_ProductWMI class (which theproductargument in yourwmiccall implicitly refers to) is best avoided, because it returns only Windows Installer-installed applications, is not query-optimized (even when filtering, all products must be enumerated), and - most importantly - invariably performs consistency checks and potentially triggers repair actions. For background information and an alternative, see this excellent blog post.The
WIMC.exeCLI is deprecated in favor of PowerShell's CIM cmdlets (see below).The only immediate problem with your code is that the version numbers returned have trailing spaces. Therefore:
However, its better to use
Get-CimInstanceto retrieve information from WMI, as it returns objects whose properties you can access:Note that
$version[0], i.e. the first object returned, contains the version number of interest, with no need to work around formatting artifacts such as trailing spaces, header lines, or empty lines (as you have to with the text-only output from external programs such asWMIC.exe).The fact that
.Versionreturns the version numbers of all objects returned by theGet-CimInstancecall is owed to PowerShell's convenient member-access enumeration feature.Get-CimInstancecall slightly by including-Property Version, which only fills in the.Versionproperty on the objects returned, you'll still need to use.Versionafterwards to get the version number.