PowerShell Where-Object Count [With Examples]

In this PowerShell tutorial, I will explain how to work with PowerShell Where-Object to get count with examples.

To count the number of objects that meet certain criteria in PowerShell, you can use the Where-Object cmdlet to filter the objects and then pipe the output to the Measure-Object cmdlet. By selecting the Count property of the result, you obtain the total number of matching objects. For example, Get-Process | Where-Object { $_.Name -eq ‘notepad’ } | Measure-Object | Select-Object -ExpandProperty Count will give you the count of all processes named ‘notepad’.

PowerShell Where-Object Count

The Where-Object cmdlet in PowerShell is used to filter objects based on their property values. It takes input from the pipeline, applies a script block or a filter expression, and outputs objects that match the criteria. The basic syntax of Where-Object is as follows:

Get-Object | Where-Object { $_.Property -Operator Value }

In this syntax, Get-Object represents any cmdlet that outputs objects to be filtered. The $_ symbol refers to the current object in the pipeline, and PropertyOperator, and Value are placeholders for the actual property you’re filtering on, the comparison operator, and the value you’re comparing against.

Count Objects with Where-Object in PowerShell

While Where-Object is great for filtering, it doesn’t inherently count the objects in PowerShell. To count the number of objects that match a certain condition, we need to combine Where-Object with the Measure-Object cmdlet. The Measure-Object cmdlet performs calculations on the property values of objects, including a count of objects.

Here is a basic example of counting objects with Where-Object and Measure-Object:

Get-Object | Where-Object { $_.Property -Operator Value } | Measure-Object | Select-Object -ExpandProperty Count

In this example, Get-Object would be replaced with the actual cmdlet that generates the objects you want to count. The Where-Object filters those objects, and Measure-Object provides the count. The final Select-Object cmdlet with the -ExpandProperty parameter displays just the count value, rather than the entire Measure-Object output.

PowerShell Where-Object Count Examples

Now. let us check out a few examples related to PowerShell Where-Object count.

Example 1: Counting Files with a Specific Extension

Let’s say you want to count the number of .log files in a directory. You could use the Get-ChildItem cmdlet to retrieve the files, filter them with Where-Object, and then count them:

Get-ChildItem -Path C:\Logs | Where-Object { $_.Extension -eq '.log' } | Measure-Object | Select-Object -ExpandProperty Count

This command will output the number of .log files in the C:\Logs directory.

Example 2: Counting Running Processes by Name

If you need to find out how many instances of a particular process, say notepad, are running, you could use the following command:

Get-Process | Where-Object { $_.ProcessName -eq 'notepad' } | Measure-Object | Select-Object -ExpandProperty Count

This will give you a count of all the notepad processes currently running.

You can see the output in the screenshot below:

PowerShell Where-Object Count

Example 3: Counting Objects with a Certain Property Value

Imagine you have a collection of custom objects and you want to count how many have a property Status set to Active. Here’s how you could do it:

$customObjects | Where-Object { $_.Status -eq 'Active' } | Measure-Object | Select-Object -ExpandProperty Count

Assuming $customObjects is an array of objects with a Status property, this command filters and counts the active ones.

Example 4: Counting Users in an OU in Active Directory

For those working with Active Directory, you might want to count the number of users in a specific Organizational Unit (OU). Using the Get-ADUser cmdlet from the ActiveDirectory module, you can do this:

Get-ADUser -Filter * -SearchBase "OU=Sales,DC=example,DC=com" | Measure-Object | Select-Object -ExpandProperty Count

This command doesn’t need a Where-Object because the Get-ADUser cmdlet’s -Filter and -SearchBase parameters already filter the users.

When dealing with large sets of data, efficiency can become a concern. Here are some tips to keep your object counting fast and efficient:

  1. Filter as Early as Possible: Apply filters at the earliest stage in your command to reduce the number of objects passed down the pipeline.
  2. Use -Filter Parameters: When available, use cmdlet-specific -Filter parameters instead of Where-Object to perform the filtering more efficiently.
  3. Avoid Unnecessary Properties: When piping to Measure-Object, exclude unnecessary properties to speed up the process.

Conclusion

In this PowerShell tutorial, I have explained how to work with “PowerShell Where-Object Count” with various examples.

You may also like: