Simple Talk is now part of the Redgate Community hub - find out why

Harnessing PowerShell’s String Comparison and List-Filtering Features

When you are first learning PowerShell, it often seems to be an 'Alice through the looking-glass' world. Just the simple process of comparing and selecting strings can seem strangely obtuse. Michael turns the looking-glass into wonderland with his wall-chart of the PowerShell string-comparison operators and syntax.


“Hmm. Wifi Hotspot!”

PowerShell is an outstanding successor to the Windows/DOS batch language. It provides a powerful and flexible framework for creating intricate scripts to automate your administrative or repetitive tasks.

The tradeoff for this added power and flexibility, though, is an increased complexity:  learning PowerShell is not a trivial undertaking. Delving into a completely new language is very much like embarking on Alice’s adventure down the rabbit-hole: you are constantly reaching unexpected impasses and taking off on  tangents, but all efforts, large and small, eventually culminate in reaching the goal-in this case, executing the most elementary of programs.

Please imagine what it is like for anyone falling into this particular rabbit hole. 


“Funny, the doors are getting
smaller, but the size of
Windows keeps growing.”

It was all very well to say “Program with me,” but the wise little Alice was not going to do that in a hurry. “No, I’ll try a few simple things first,” she said, “and see whether it’s poisonous or not”; for she had read several little stories about programmers who had got burnt, and eaten up by ridiculously obtuse languages, and other unpleasant things, all because they would not remember the simple rules their friends had taught them, ‘see if it is right for you’:

“Let’s try listing all the files in the current directory. Well, just type… hmm, how do you do that in PowerShell? Well, in batch that is just dir… hey, that works! Good heavens, even the Unix ls command works.”

 Both of those sound like convenient aliases, Alice surmised. “Well, aliases to what? Let’s check the help system. Hmm, how do you access help? … Aha! The get-help cmdlet. The alias help can be accessed with get-help about_aliases.”  From there Alice found that …
      Get-Alias -name dir
… reveals the underlying cmdlet, Get-ChildItem. “Great. Now how to exclude binaries, i.e. .dll or .exe? Check the parameters to Get-ChildItem in the help system.” She then stumbled over the fact that you could even ask for help on the dir alias to get the same details… but Alice felt she was digressing.

“So now”, she mused, “I know that …
      Get-ChildItem -exclude *.dll,*.exe
 … will answer the original question. Now, let’s add another constraint: do not output anything unless there is a file named ‘Process_Me’ in the directory. Clearly a simple conditional should suffice. In many languages, that would be an if statement. In PowerShell, what is a statement? Is that a cmdlet? Or perhaps a command?”


I started out in Accountancy,
but then discovered Lisp

After experimenting with the help system for a while, she realized that cmdlets tend to have the form verbnoun (e.g. Get-ChildItem) while language constructs (if, switch, etc.) are commands. Command help is accessed via get-help about_command. So checking about_if, shows it is a fairly standard if/else. There is also an elseif for multiple conditions. The test condition itself must be in parentheses. But what of the form of the test condition itself? Should it be …
  ($name = "Process_Me")

… or …
  ($name == "Process_Me")
…or ...
  ($name === "Process_Me")?

Alice was surprised to find that it was none of these. The help page shows just two trivial examples: -gt for greater than and -eq for equals. Curious, she thought, but OK, what about the rest? No information on that page. It does, at least, give a hint to read up on about_Comparison_Operators to learn more. On that page she found some further details and some further references… quite a lot of material to digest-though not terribly taxing-before she could write a simple conditional!

Particularly intriguing to me is PowerShell’s core fundamentals of string handling with respect to conditionals. Though the PowerShell help system is better than some, following the above approach will still not get you a complete picture of understanding string comparisons, when you consider exact vs. wildcard vs. regex matches, negations, substrings… and that is just for scalar strings. PowerShell also provides arrays, and you can pass arrays through pipelines and filters, etc. The accompanying wallchart conglomerates every bit of this information about strings into a single page reference that should make using this core area of PowerShell quick and easy.


The full PDF wallchart can be downloaded below. You can view the wallchart in a browser, as HTML, by clicking the image of it

How you log in to Simple Talk has changed

We now use Redgate ID (RGID). If you already have an RGID, we’ll try to match it to your account. If not, we’ll create one for you and connect it.

This won’t sign you up to anything or add you to any mailing lists. You can see our full privacy policy here.


Simple Talk now uses Redgate ID

If you already have a Redgate ID (RGID), sign in using your existing RGID credentials. If not, you can create one on the next screen.

This won’t sign you up to anything or add you to any mailing lists. You can see our full privacy policy here.