Development Powershell Tutorials

Powershell – Where are we going

Media talks a lot about flattening curves so lets flatten Powershell’s learning curve by talking about the cmdlet Where-Object!

Where-Object (aliases: Where or a question mark “?”) is used to filter the data from your object based on a statement.

Lets look at an example:

Get-Service | Where-Object { $_.status -ne "Running" -and $_.StartType -eq "Automatic" }

First we get all services on the local computer, then we pipe it to the Where-Object cmdlet.
We give Where-Object a statement that will filter the output only showing services with a Status that is not “Running” and that has their StartType set to “Automatic”.

Something to note is that filtering output should be done as far left in the pipe as possible, which means as soon as possible.
The less data Powershell have to process, the less strain it puts on performance and it will also run faster.

If the cmdlet you are running has built in filters like Get-ADUser -filter then it’s probably better to use them, although sometimes those filters can’t do everything that Where-Object can.

What is -ne and -eq? They are operators; -ne = NotEqual, -eq = Equal.
If you are familiar with other languages then you might understand =/= and == which is the same, just different syntax in Powershell.
Not all operators are shortend in Powershell, -Like, -NotLike or -Contains are a few examples.
For the complete list view Microsoft’s documention about_comparison_operators

That’s it for now, be safe out there and as always, continue to learn and evolve your skills, see you in the next one!

Do you want to know more? Here is a list of tutorials to check next


Leave a Reply

Your email address will not be published. Required fields are marked *