Testing PowerShell string equality


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 = ""

PS C:\> $version[2]
PS C:\> $versionString
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

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.



First, a warning:

  • The Win32_Product WMI class (which the product argument in your wmic call 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.exe CLI 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:

# .Trim() removes leading and trailing whitespace.
$version[2].Trim() -eq $versionString

However, its better to use Get-CimInstance to retrieve information from WMI, as it returns objects whose properties you can access:

$version = (
  Get-CimInstance Win32_Product -Filter "caption like '%Citrix Workspace Inside%'"

$version[0] -eq $versionString

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 as WMIC.exe).

The fact that .Version returns the version numbers of all objects returned by the Get-CimInstance call is owed to PowerShell’s convenient member-access enumeration feature.

  • As an aside: While you could improve the efficiency of the Get-CimInstance call slightly by including -Property Version, which only fills in the .Version property on the objects returned, you’ll still need to use .Version afterwards to get the version number.

Answered By – mklement0

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published