Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Converting VBScript's Is Operator


Windows PowerShell


Definition: Compares two object reference variables.

Is Operator

In VBScript the IS operator is used to determine whether two object references refer to the same object. This differs from Windows PowerShell, in which the IS operator is used to determine whether a variable has a specified data type. (In other words, it all depends on what the definition of “is” is.)

That doesn’t mean that you can’t compare objects in Windows PowerShell; you can. It just means is that you need to use the Compare-Object Cmdlet to carry out this task. In the following example, we create two instances of the Scripting.Dictionary object. We then use Compare-Object to compare the two object references, tacking on the -includeequal parameter to ensure that we’ll get back an answer even if the two objects are identical:



$a = new-object -comobject scripting.dictionary
$b = new-object -comobject scripting.dictionary
$c = compare-object $a $b -includeequal

When you run this command and then echo back the value of $c you should get the following:



InputObject                                                 SideIndicator
-----------                                                 -------------
{}                                                          ==

The == SideIndicator means that the two objects are equal.

Here’s a modified example in which the two objects are not equal: one references the Scripting.Dictionary object, while the other references the Scripting.FileSystem object. Here are the commands:



$a = new-object -comobject scripting.dictionary
$b = new-object -comobject scripting.filesystemobject
$c = compare-object $a $b -includeequal

And here’s what we get back when we run these commands and then echo the value of $c:



InputObject                                                 SideIndicator
-----------                                                 -------------
System.__ComObject                                          =>
{}                                                          <=

Although it is somewhat meaningless when working with COM objects, the => and <= symbols indicate which of the two objects contain a given item (such as an individual line in a text file). In this case the important thing is that we do not see the == SideIndicator, which means that the two objects are not identical.


Return to the VBScript to Windows PowerShell home page

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.