PowerShell where-object regex [With examples]

In this PowerShell tutorial, I will discuss everything about the PowerShell where-object regex. I will show you how to use regex with where-object in PowerShell with examples.

To use regular expressions (regex) with the PowerShell Where-Object cmdlet, you utilize the -match operator inside the script block to filter objects based on regex patterns. For example, to find files with a .txt extension, you would use Get-ChildItem | Where-Object { $_.Name -match ‘.txt$’ }, where Get-ChildItem retrieves the files and Where-Object filters them using the regex pattern that matches any string ending in .txt.

PowerShell where-object regex

The Where-Object cmdlet in PowerShell is used to filter objects from a collection based on their property values. This cmdlet is often used in a pipeline, where it receives input from a preceding command and filters objects based on a script block condition.

When you need to filter text or strings based on patterns, regular expressions within a Where-Object script block can be incredibly effective. The -match operator is commonly used within the script block to test each input object against a regex pattern.

Basic Syntax

The basic syntax for using regex with Where-Object looks like this:

collection | Where-Object { $_ -match 'regexPattern' }

Here, collection is the input data, $_ represents the current object in the pipeline, and 'regexPattern' is your regular expression.

Examples of PowerShell where-object regex

Let’s look at some practical examples to demonstrate the use of regex with Where-Object.

Example 1: Filtering File Names

Imagine you have a directory of files, and you want to filter out files that have a specific file extension, say .txt.

Get-ChildItem -Path "C:\MyFolder\" -File | Where-Object { $_.Name -match '\.txt$' }

The regex pattern '\.txt$' looks for strings that end with .txt, ensuring you only get files with that extension.

Here is the output. You can see the screenshot below:

PowerShell where-object regex

Example 2: Validating Email Addresses

Suppose you have a list of email addresses, and you want to find those that are incorrectly formatted.

$emails | Where-Object { $_ -notmatch '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' }

The regex pattern here is a common one for matching email addresses, and the -notmatch operator is used to filter out those that do not conform to the pattern.

Example 3: Searching for IP Addresses

If you have a log file and you want to extract lines that contain an IP address, you could use the following command:

Get-Content log.txt | Where-Object { $_ -match '\b\d{1,3}(\.\d{1,3}){3}\b' }

The regex pattern '\b\d{1,3}(\.\d{1,3}){3}\b' is designed to match the typical format of an IP address.

Conclusion

Regular expressions are a powerful tool in text processing, and when combined with PowerShell’s Where-Object cmdlet, they become even more powerful. Whether you’re filtering file names, validating data, or searching through logs, understanding how to use regex with Where-Object can greatly enhance your PowerShell scripting capabilities.

I have explained how to use the PowerShell where-object regex in this PowerShell tutorial with a few examples.

You may also like: