PowerShell: How to Use Get-Content with Filter


You can use the Get-Content cmdlet in PowerShell to retrieve the content from a particular file.

Often you may want to filter the results from the Get-Content cmdlet to only display the lines from a file that contain particular patterns.

You can use the following methods to do so:

Method 1: Use Get-Content and Filter Based on One String

Get-Content employees.txt | Select-String -pattern $my_pattern

This particular example will retrieve all lines from the file named employees.txt that contain the pattern specified in the $my_pattern variable.

Method 2: Use Get-Content and Filter Based on Multiple Strings

Get-Content employees.txt | Select-String -pattern $my_patterns

This particular example will retrieve all lines from the file named employees.txt that contain any of the patterns specified in the $my_patterns array.

The following example shows how to use each of these methods in practice.

Example 1: Use Get-Content and Filter Based on One String

Suppose we have a file named employees.txt in our current directory that contains the ID values for various employees at some company.

We can use the Get-Content cmdlet to view all of the lines from this file:

Suppose that we would like to filter for only the lines in the file that contain the pattern “A00” somewhere in the employee ID value.

We can use the following syntax to do so:

$my_pattern = "A00"
Get-Content employees.txt | Select-String -pattern $my_pattern

The following screenshot shows how to use this syntax in practice:

PowerShell Get-Content filter

Notice that this returns only the lines from the file that contain the pattern “A00” somewhere in the line.

If you’d like to return only the content without any empty lines, you can filter for the Line property:

$my_pattern = "A00"
Get-Content employees.txt | Select-String -pattern $my_pattern | % { $_.Line }

The following screenshot shows how to use this syntax in practice:

PowerShell Select-String return no empty lines

This returns only the lines from the file that contain the pattern “A00” and no empty lines before or after the content.

Example 2: Use Get-Content and Filter Based on Multiple Strings

Suppose that we would like to filter for the lines in the employees.txt file that contain any of the following patterns somewhere in the employee ID value:

  • A00
  • B14
  • C00

We can use the following syntax to do so:

$my_patterns = @("A00", "B14", "C00")
Get-Content employees.txt | Select-String -pattern $my_patterns

The following screenshot shows how to use this syntax in practice:

PowerShell Get-Content filter based on multiple patterns

Notice that this returns only the lines from the file that contain one of the three patterns that we specified.

Note: You can find the complete documentation for the Get-Content cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: Use Get-Content and Skip First N Lines
PowerShell: How to Use Get-ChildItem with Filter
PowerShell: Use Get-ChildItem with Multiple Filters

Leave a Reply