Log on to the Windows 7 Enterprise client
Task 1: Test Equality
To test if two values are equal to or not equal to each other, you can use the –eq or –ne operators respectively. These operators, like all comparison operators, are case-insensitive by default.
1. Open the PowerShell console or ISE and type the following command:
“PowerShell” –eq “powershell”
This command will return a value of True indicating that these two string values are equal to each other.
2. You can also test if two values are not equal by running the following command:
“POWERSHELL” –ne “powershell”
This command returns a value of False since PowerShell is case-insensitive and, irrespective of the case, by default two strings of the same character combination and order will be the same.
3. If you want PowerShell to perform a case-sensitive test of equality, you can add a ‘c’
before the operator you are using.
“PowerShell” –ceq “powershell”
This will return a value of False since the strings have different casing. This will work with any comparison operator, not just –eq and –ne.
4. PowerShell can be made to explicitly do a case-insensitive comparison, which can help readability in scripts in certain cases. This is done by using an i instead of a c.
“PowerShell” –ieq “powershell”
This will return True once again since an insensitive comparison was explicitly used.
5. Testing equality does not stop with -eq and -ne. You may also need to know whether something is less than, greater than, and so on. When working with numbers, these operations function exactly as you would expect; the following commands:
4 –gt 4 3 –ge 3 3 –lt 7 7 –le 4
The commands above will return the values False, True, True, False, respectively, for tests of greater than, greater than or equal to, less than, and less than or equal to operators.
These commands will return the values True, False, True, and False, respectively. When comparing string values, PowerShell uses the Compare method built into the String type.
Therefore, in a case-sensitive comparison, “u” is considered less than “U”.
7. It is interesting to note that you can use arrays when testing for equality. When you do this, you will get more than a simple True or False return value.
“foo”,”bar”,”baz”,”qux” –eq “foo”
“fizz”,”buzz”,”bang” -ne “buzz”
“one”,”two”,”three” –eq “four”
In the example shown above:
Instead of returning a simple True, the first command returns “foo” indicating that it found that value to be equal to the one being tested against
The second command returns an array containing “fizz” and “bang” indicating that it found those values to be not-equal to “buzz”
The last command returns no data because nothing in the array is equal to “four”
Task 2: Further Tests with Arrays
While equality is easily tested against an array and will return the appropriate set of values, sometimes only a True or False value is required that can be used simply in a conditional statement.
1. To check if a specific value is contained within an array, you can use the -contains operator, returning True if the specified value is found, otherwise false.
“foo”,”bar”,”baz”,”qux” –contains “bar”
“one”,”two”,”three” –contains “four”
In the example shown above:
The first command will return a value of True since “bar” is contained within the array
The second command will return False since “four” is not contained within the array 2. To check if a value is not contained within an array, you can simply change the operator
to -notcontains instead. Use the same data as our previous example, but with the new operator.
“foo”,”bar”,”baz”,”qux” –notcontains “bar”
“one”,”two”,”three” –notcontains “four”
This will then return False for the first command and True for the second.
Task 3: Wildcard and Regular Expression Tests
Checking for a specific value being equal to another is not always enough to confirm if you are dealing with the right data. Sometimes you will need to match a string based on only a part of it. For example, looking at a list of usernames and only returning those that begin with the letter “P”.
PowerShell allows you to use basic wildcards through the -like and -notlike operators, and allows you access to regular expressions through -match and -notmatch.
1. Wildcard characters allow you to represent more than a single character when matching.
“Dog” –like “D*”
“Dug” –like “D[ou]g”
“Dig” –notlike “D?g”
“Doug” –like “D[a-z]g”
In the example shown above:
The first returns true because the “*” character is a wildcard that matches zero or more characters of any kind
The second returns true because the square brackets denote a group of characters to match a single time, in this case “o” or “u”
The third returns false because although you allow any single character with the “?”
wildcard character you are using the notlike operator
The fourth also returns false but this time it uses a range wildcard to match any single letter from “a” to “z”, but here you have two characters in the middle
2. Just like other comparison operators, the like and notlike operators can be forced to be case sensitive by adding a “c” to the operator name
“Cat” –clike “C*”
“Cat” –clike “c[au]t”
“curl” –clike “c[au]*”
These three commands will return true, false, and true, respectively.