Where do you want to go? Get just what you want with PowerShell Where

Getting just what you want makes your life easy — not just in the real world but in the world of programming too. Imagine a situation where you want to know how many employees joined your company over the last decade and specifically, how many of them have the first name John. To do this, you’ll have to get the list of all the employees who joined over 10 years and search through this list for the name “John.” While this is not so tough when you have 100 records, it can get mind-boggling when you have to deal with thousands of records, not to mention the extra time it will take for this search. In such situations, you’re better off using PowerShell Where, a method that filters your data to give you just what you want!

What is PowerShell Where?

In a nutshell, PowerShell Where helps you to filter through a list to get what you want quickly. More importantly, this command takes input from other cmdlets and filters to give you specific information. In this sense, PowerShell Where customizes the output of a program.

Syntax

The syntax for PowerShell Where is:

Where-Object [-filterscript] {script} [-inputObject object] [CommonParameters]

Breaking it down:

[-filterscript] {script} gives a boolean true or false value and helps to determine which values should be allowed to pass. In other words, you lay down the condition for the filter and only those values that meet this condition are given as output.

[-inputObject object] is the object that has to be filtered and it could be a list, file, output of a cmdlet, and more.

[CommonParameters] is a set of parameters that are supported by all PowerShell cmdlets. These parameters help to customize your results and overall programming. Some examples of common parameters are:

  • -Confirm: A boolean value that takes confirmation from users before continuing with the program.
  • -Debug: Gives programming-based information to identify and debug an error.
  • -ErrorAction: Controls the behavior when an error occurs and tells the program to Continue, Stop, Suspend, Ignore, Inquire, or SilentlyContinue after the error.
  • -ErrorVariable: An object for error details.
  • -OutBuffer: The number of objects available in the buffer before the next cmdlet is executed
  • -Verbose: Gives detailed information about the operation
  • -WarningAction: Determines how a program should respond when it encounters a warning message
  • -WarningVariable: A variable to store the warning

PowerShell Where comes with an alias “?”, so you can either say PowerShell Where-Object or PowerShell ?-Object and the results will be the same. Also, you can use Where instead of Where-Object or simply “?.” So, the acceptable variations are:

  • Where-Object
  • Where
  • ?

They can be used interchangeably.

A technical note here. Since we are using the output of another cmdlet into the Where method, we often use the word “piped” instead of input. So, it is common practice to say that the output of Get-Process is piped to the Where method to find the exact information.

Example

This statement gives you a list of all the system processes that are stopped:

Get-Service | Where-Object {$_.status -eq ‘Stopped’}

powershell where

In the above example, the Get-Service method returns all the system processes while the Where method filters this output and presents only those processes that have the stopped status.

Uses of PowerShell Where

PowerShell Where is a highly useful and versatile method that you can use across different situations. It’s popularity also stems from the fact that it supports regular expressions, comparison operators, and more to customize the output of a program to get the exact information you want.

For example, you can get a Windows service with the name “SNMP” if it is in the “running” state:

Get-Service | Where-Object {($_.status -eq "running") -and ($_.name -eq "SNMP")}

powershell where

Here, “$_” represents the default value that’s passed through the pipeline.

Another example is to find the processes that have more than 100 handles:

Get-Service | Where-Object {$_.handles -gt 100}

PowerShell Where supports all comparison operators and regular expressions, so you can use it for processing.

SelectionMode values

Another cool aspect of PowerShell Where is that you can use SelectionMode values to get to the record you want. Some of the SelectionMode values are:

  • First: Stops processing after the first match is found.
  • Last: Returns the last matching record.
  • SkipUntil: Skips all the records until your conditions are met and returns the rest of the records.
  • Split: Returns two arrays, one containing a set of matched elements and the other contains the remaining elements.
  • Until: Returns all the elements until the condition is met and stops processing after that.

The above values are powerful and can add greatly to your programming.

Your choices are endless and you can do a whole lot of things by taking the output of another program and piping it through the Where method.

Exporting data

Another situation where PowerShell Where will come in handy is to export data from a command to a file after filtering it with certain conditions.

For example, I can write the values of the active system processes to a text file like this:

Get-Service | Where {$_.status -eq "Running"} | Out-file c:\activesystemprocess.txt

Like this, you can export it to HTML or CSV file, depending on your requirements.

In all, the above uses are just to give you an idea of the versatility and power of this command and can come in handy in a ton of situations.

Common best practices of PowerShell Where

Here are a few common best practices that most programmers use for easy code flow and readability.

  • Though it is acceptable to use “?,” using Where makes your code more readable.
  • Make sure to have the “|” symbol before using Where and there should be no other code between the pipe symbol and Where.
  • Use sensible variable names and capitalize them whenever needed, though PowerShell doesn’t insist on it.
  • Add comments for easy reading and tracking.

As you can see, PowerShell Where is a versatile function that can truly reduce your code and get powerful results just the way you want.

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top