Check if a File Contains a Specific String Using PowerShell

  1. Check if a File Contains a Specific String Using Select-String in PowerShell
  2. Check if a File Contains a Specific String Using Get-Content in PowerShell
  3. Check if a File Contains a Specific String Using Get-ChildItem and Search-String in PowerShell
Check if a File Contains a Specific String Using PowerShell

This article discusses the various methods we can employ to find a string in a text file with Windows PowerShell.

We can use grep in Unix and Linux environments to achieve this. The equivalent for this in PowerShell is a cmdlet called Select-String, which uses regular expressions to locate strings in files.

Check if a File Contains a Specific String Using Select-String in PowerShell

Let’s say we have a Demo.txt file on our machine. How would we find the string Demonstration in the file using PowerShell?

Here is the PowerShell script you can use:

Select-String -Path C:\Users\pc\Demo\Demo.txt -Pattern 'Demonstration'

In the shell script illustrated above, Select-String employs the pattern Demonstration to search for the string in our Demo.txt file. Note that we have specified the full path to our file in the Path parameter.

If our file contains the specified pattern, the console will output the file name, the line number, and the line containing the string. Here is the output:

C:\Users\pc\Demo\Demo.txt:1:This purely for demonstration

From the output above, we have the Demo.txt file with its full path, the line containing our string, line 1, and the line containing the string, i.e., This is purely for demonstration.

What if we only need to know if the file contains or does not contain the string?

In such a case, you can use a script like the one shown below:

$Knw = Select-String -Path C:\Users\pc\Demo\Demo.txt -Pattern "Demonstration"

if ($Knw -ne $null)
{
    echo Contains String
}
else
{
    echo Does not Contain String
}

In the shell script above, we have defined a string called $Knw and checked if it is not equal to $null. This is true since our file does contain the pattern Demonstration.

We should get the Contains string as the output.

Check if a File Contains a Specific String Using Get-Content in PowerShell

Get-Content is a cmdlet that fetches the contents of a specified path to a file and reads the contents of the file to return a string object.

To use Get-Content in our Demo.txt file, we will use the script illustrated below:

Get-Content -Path C:\Users\pc\Demo\Demo.txt | Select-String -Pattern "Demonstration"

In the PowerShell script above, Get-Content fetches and reads the contents of our Demo.txt file specified by the Path parameter. It then forwards the file’s content to the Select-String command that employs the Pattern parameter to locate our Demonstration string.

This will output the line containing our string.

This purely for demonstration

If we only want to confirm the presence of the string, we can use the script shown below:

If (Get-Content C:\Users\pc\Demo\Demo.txt | %{$_ -match "Demonstration"})
{
    echo Contains String
}
else
{
    echo Does not Contains String
}

Since our file contains the specified string, we should get the Contains String as the output.

Check if a File Contains a Specific String Using Get-ChildItem and Search-String in PowerShell

In a scenario where we have a pool of over fifty text files, and we would like to search for a string in all the files, how would we go about it?

In such a scenario, we will employ the Get-ChildItem and the Search-String cmdlet. The Get-ChildItem cmdlet will find one or more files based on our search criteria.

Let’s look at an example.

Assuming our Demo folder has over fifty text files, how do we search for the string Demonstration in these files?

Here is the script we will use:

Get-ChildItem -Path C:\Users\pc\Demo\ -Recurse | Select-String -Pattern 'Demonstration'

The script above will search for the string Demonstration recursively in the specified Path parameter. The console will output the file names containing our string, the line numbers, and the lines containing the string.

Here is our output:

C:\Users\pc\Demo\Demo.txt:1:This purely for demonstration
C:\Users\pc\Demo\Insert.txt:1:We will use this demonstration for a test trial
C:\Users\pc\Demo\myfile.txt:1:This is a demonstration file for Get-ChildItem

There you have it.

In a nutshell, the Get-ChildItem, Get-Content, and Search-String commands come in handy when we want to find a specific file string using Windows PowerShell. Select-String is the equivalent of grep in Unix and findstr in Windows.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - PowerShell String

  • Array of Strings in PowerShell
  • Extract a PowerShell Substring From a String
  • Extract Texts Using Regex in PowerShell
  • Generate Random Strings Using PowerShell
  • Escape Single Quotes and Double Quotes in PowerShell