---
title: "PowerShell Grep: Unleash the Power of Text Search"
description: "Discover the grep-like capabilities of PowerShell for powerful text search and manipulation in this informative tutorial."
canonical: "https://adamtheautomator.com/powershell-grep/"
---

# PowerShell Grep: Unleash the Power of Text Search

> Discover the grep-like capabilities of PowerShell for powerful text search and manipulation in this informative tutorial.

Source: https://adamtheautomator.com/powershell-grep/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![PowerShell Grep: Unleash the Power of Text Search](https://adamtheautomator.com/wp-content/uploads/2020/08/Grep-of-Powershell.png)

# PowerShell Grep: Unleash the Power of Text Search

[![](https://secure.gravatar.com/avatar/f08b754bc0dce1c76685f6afa93185ecfb6f861fd14f993286e06bba69eaeb8b?s=192&d=mm&r=g)Adam Listek](https://adamtheautomator.com/author/alistek/)13 August 20207 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Grep](/tag/grep/)[PowerShell](/tag/powershell/)

Table of Contents

*   [Exploring the Select-String Cmdlet](#h-exploring-the-select-string-cmdlet)
*   [Parameters of Select-String](#h-parameters-of-select-string)
*   [Using PowerShell Grep err… Select-String](#h-using-powershell-grep-err-select-string)
*   [Simple Matching in Files](#h-simple-matching-in-files)
*   [More Complex RegEx Matching](#h-more-complex-regex-matching)
*   [Searching with Context](#h-searching-with-context)
*   [Conclusion](#h-conclusion)

One of the first Linux commands that many system administrators learn is grep. This venerable tool has been around for decades and is crucial to any administrator’s toolbelt. Grep’s core is simply the ability to search plain text for a RegEx pattern. Grep can search files in a given directory or streamed input to output matches. Did you know PowerShell has grep? Well..almost.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

[PowerShell](https://adamtheautomator.com/tag/powershell/), being a language, is more than just a single purpose binary. Therefore what built-in abilities exist to search for plain text using RegEx patterns much like grep does? In this article we explore the myriad ways to search for text in files using PowerShell.

## Exploring the `Select-String` Cmdlet

[`Select-String`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7) (our PowerShell grep) works on lines of text and by default will looks for the first match in each line and then displays the file name, line number, and the text within the matched line. Additionally, `Select-String` can work with different file encodings, such as Unicode text, by use the byte-order-mark (BOM) to determine the encoding format. If the BOM is missing, `Select-String` will assume it is a UTF8 file.

### Parameters of `Select-String`

*   `AllMatches` – Normally, `Select-String` will only look for the first match in each line, using this parameter the cmdlet will search for more than one match. A single `MatchInfo` object will still be emitted for each line, but it will contain all of the matches found.
*   `CaseSensitive` – Matches are not case-sensitive by default, this forces the cmdlet to look for matches that match exactly to the input pattern.
*   `Context` – A very useful parameter in that, you can define the number of lines before and after the match that will be displayed. Adding this parameter modifies the emitted `MatchInfo` object to include a new `Context` property that contains the lines specified.

> _Keep in mind that if you pipe the output of `Select-String` to another `Select-String` call, the context won’t be available since you are only searching on the single resulting `MatchInfo` `line` property._

*   `Culture` – Used with the `SimpleMatch` parameter, this specifies a culture to be matched with the specified pattern. This includes options such as `en-US`, `es`, or `fr-FR` as examples. A few other useful options is the `Ordinal` and `Invariant` options. `Ordinal` is for non-linguistic binary comparisons and `Invariant` is for culture independent comparisons.

> _This parameter was introduced in PowerShell 7 and is not available to prior versions. Also keep in mind that this will use the current culture of the system, by default, which can be found using `Get-Culture`._

*   `Encoding` – Specify the encoding of the target file to search, which a default of `utf8NoBOM`.
    
    *   `ascii`: Uses the encoding for the ASCII (7-bit) character set.
    *   `bigendianunicode`: Encodes in UTF-16 format using the big-endian byte order.
    *   `oem`: Uses the default encoding for MS-DOS and console programs.
    *   `unicode`: Encodes in UTF-16 format using the little-endian byte order.
    *   `utf7`: Encodes in UTF-7 format.
    *   `utf8`: Encodes in UTF-8 format.
    *   `utf8BOM`: Encodes in UTF-8 format with Byte Order Mark (BOM)
    *   `utf8NoBOM`: Encodes in UTF-8 format without Byte Order Mark (BOM)
    *   `utf32`: Encodes in UTF-32 format.
    
    Starting with PowerShell Core 6.2, the `Encoding` parameter also accepts numeric IDs of registered code pages such as `1251` or string names such as `windows-1251`.
    

> _Starting with PowerShell Core 6.2, the `Encoding` parameter also accepts numeric IDs of registered code pages such as `1251` or string names such as `windows-1251`._

*   `Exclude` – Working with the `Path` parameter, exclude specific items using a pattern, such as `*.txt`.
*   `Include` – Just like the `Exclude` parameter, `Include` will include only the specified items using a pattern, such as `*.log`.
*   `List` – Only return the first instance of matching text from each input file. This is intended to be a fast and efficient way to retrieve a listing of files that have matching contents.
*   `LiteralPath` – This tells `Select-String` to use the values as input, instead of interpreting values such as `*` as a wildcard. If the path includes escape characters, enclose them in single quotation marks to do no interpretation.
*   `NoEmphasis` – Instead of highlighting the string that the pattern is matched upon, disable the highlighting of matches. By default, emphasis uses negative colors based on the background text colors.
*   `NotMatch` – Look for text that does not match the specified pattern.
*   `Path` – Specify the path to the files to search. Wildcards are permitted, but you cannot specify only a directory. The default is the local directory.
*   `Pattern` – The pattern to search the input content or files for based on RegEx.
*   `SimpleMatch` – Use a simple match instead of regular expressions. Since RegEx is not used, the returned `MatchInfo` object does not have any values in the `Matches` property.
*   `Raw` – Output the matching strings, without a `MatchInfo` object. This is the behavior that is most similar to `grep` and not the more object oriented nature of PowerShell.
*   `Quiet` – Only return a `$true` or `$false` if the pattern is found.

## Using PowerShell Grep err… `Select-String`

Of course knowing how the parameters and options of a cmdlet work is not quite the same as using it in a production environment. Let’s dive into examples and see how we can leverage `Select-String` to make find text matches easier.

There are three ways that we can use `Select-String` to find matches.

*   Pipe in quoted text to the `Select-String` cmdlet, i.e. stream in the text.
*   Using text stored in a variable, pass the variable to the `InputObject` parameter.
*   Use the `Path` parameter to specify files to search for the text in.

> The files we are using for testing this are randomly generated content, but of the same type as often found in production systems.

### Simple Matching in Files

Starting off with a very simple example, let us look for `Joe` in a handful of CSV files.

```powershell
Select-String -Path "Users\*.csv" -Pattern "Joe"
```

![Demonstrating a simple Select-String pattern match.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-21.png)

Demonstrating a simple Select-String pattern match.

As you can tell, this is pretty simple, we see how `Joe` is highlighted on the line with the rest of the data. But what data is actually being returned here? Let us take a look at all of the properties of a returned match.

```powershell
Select-String -Path "Users\*.csv" -Pattern "Joe" | Select-Object * -First 1
```

![Powershell Grep : Showing the returned properties from a Select-String match.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-22.png)

Powershell Grep : Showing the returned properties from a Select-String match.

We have a couple of properties here that are useful. Notably the `line`, `path`, `pattern`, and `matches`. Most of what we want to know is in the `matches` property.

```powershell
Select-String -Path "Users\*.csv" -Pattern "Joe" | Select-Object -ExpandProperty Matches -First 1
```

![Enumerating the Matches property and available data.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-23.png)

Enumerating the Matches property and available data.

Here you can see how even though we used a simple expression, this is still a RegEx expression and the subsequent details available.

What if we look for several different values using comma separated patterns? This is useful as this actually defines three different patterns and not one complex RegEx value.

```powershell
Select-String -Path "Users\*.csv" -Pattern "Joe","Marti","Jerry"
```

![Returning multiple matches from a Select-String search.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-24.png)

Returning multiple matches from a Select-String search.

You can see how this is the case, if we select only the `filename`, `pattern`, and `line` from our search.

```powershell
Select-String -Path "Users\*.csv" -Pattern "Joe","Marti","Jerry" | Select-Object FileName, Pattern, Line
```

![Filtering the results from a multiple Select-String match.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-25.png)

Filtering the results from a multiple Select-String match.

### More Complex RegEx Matching

Now that we demonstrated some of the simpler matching methods, what about utilizing RegEx more to actually look for more useful patterns? The three examples here are looking for Email Addresses, IP Addresses, and Social Security Numbers (SSNs). The patterns used here are not the only way to construct a RegEx search, and there may be easier ways. PowerShell Grep (Select-String) is a pretty advanced cmdlet.

Let’s look to see if emails are contained in our files. Using a somewhat complex RegEx match, as shown below, will demonstrate finding those matches.

```powershell
Select-String -Path "Users\*.csv" -Pattern '\\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b' | Select-Object -First 10
```

![Demonstrating using RegEx to match data.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-26.png)

Demonstrating using RegEx to match data.

Of course, of more concern might be if there were SSNs included in a file. A very simple match for this would be the following.

```powershell
Select-String -Path "Users\*.csv" -Pattern '\d\d\d-\d\d-\d\d\d\d' | Select-Object -First 10
```

![Demonstrating a simple SSN RegEx search.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-27.png)

Demonstrating a simple SSN RegEx search.

Finally, what if we wanted to look up some IP Addresses in our file? Using another RegEx expression to look for that pattern, makes quick work.

```powershell
Select-String -Path "Users\*.csv" -Pattern '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' | Select-Object -First 10
```

![Demonstrating a simple IP Address RegEx search.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-28.png)

Demonstrating a simple IP Address RegEx search.

A caveat about this RegEx. Technically, this will match values up to `999.999.999.999` which is an invalid IP. You can construct more accurate RegEx expressions that will get longer, but it is a trade off depending on what you are looking to do.

### Searching with Context

Context is very useful in troubleshooting, it helps to explain what is happening prior to an event occurring and after. For example, let’s search in an Apache log and find this `suspendedpage.cgi` text.

```powershell
Select-String -Path "Web\*.txt" -Pattern "suspendedpage.cgi" -Context 1 | Select-Object -First 1
```

![Searching for a line in an Apache log.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-29.png)

Searching for a line in an Apache log.

The `>` simple indicates the matched line, and there is one line prior to the match and after the match. In this example, this could tell us that the Google bot was looking for `robots.txt` and unfortunately received a `suspendedpage.cgi` result instead. Next, it went to try the homepage and perhaps got the same error.

What exactly is contained in the `context` property then as emitted by the `MatchInfo` object? If we expand upon that property, you can see that there is `PreContent` and `PostContent`. This means you can manipulate this further down the line if necessary.

```powershell
Select-String -Path "Web\*.txt" -Pattern "suspendedpage.cgi" -Context 1 | Select-Object -ExpandProperty Context -First 1 | Format-List
```

![Demonstrating the Context property.](https://adamtheautomator.com/wp-content/uploads/2020/08/Untitled-30.png)

Demonstrating the Context property.

Other examples of searching through log files are in articles such as [Making Sense of the Microsoft DNS Debug Log](https://adamtheautomator.com/dns-debug-log-parser/) which demonstrates using `Select-String` to look through a DNS debug log. The PowerShell grep is strong in that post.

## Conclusion

[Grep](https://adamtheautomator.com/windows-subsystem-for-linux/) is an incredibly useful tool in the Linux world, and `Select-String` offers much of the same functionality in the PowerShell world. Adding in the object-oriented nature of PowerShell only serves to enhance the utility and usefulness that the cmdlet offers.

For many System Administrators, being able to quickly and efficiently search log files over varying types, along with understanding context, is an incredibly important and necessary ability. PowerShell `Select-String` makes this easy to do and saves countless hours of troubleshooting!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-grep%2F&text=PowerShell%20Grep%3A%20Unleash%20the%20Power%20of%20Text%20Search)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-grep%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-grep%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
