Table of Contents
Using Select-String
Cmdlet
Use the Select-String cmdlet to check if the specified file contains the given string in PowerShell.
1 2 3 4 5 6 7 8 9 10 |
$path = "E:\Test\file1.txt" $string = "customers" $result = Select-String $path -Pattern $string if ($result -ne $null) { Write-Host "The specified text file contains the string '$string'." } else { Write-Host "The specified text file does not contain the string '$string'." } |
1 2 3 |
The specified text file contains the string 'customers'. |
First, we declared and initialized a variable named $path
containing the location of the file1.txt
file. Then, we created another variable called $string
and set its value with "customers"
; this is the value we were looking for in the file1.txt
.
After that, we used Select-String with -Pattern
parameter to search the specified $string
in the file that was located in $path
and saved the returned value in $result
variable.
The Select-String
cmdlet is used to find the specified string in other strings and files; we can use the exact string we are looking for or define a regular expression to match the pattern. We use the -Pattern
parameter to mention the search string or pattern.
The Select-String
depends on the text’s lines; by default, it finds the first match in every line of the text, and for every match, it displays the file’s name, line number, and complete text having the match. As we are just concerned about whether the search string is found, we used the if-else
block with the -ne
operator to check if the $result
variable is not equal to $null
.
If not, we used the Write-Host
cmdlet to print a message saying the string is found; otherwise, we printed a message informing the string is not found.
The
Select-String
cmdlet is similar to thegrep
command in UNIX orfindstr.exe
in Windows Operating System.
By default, the Select-String
with -Pattern
option performs a case-insensitive match, which means the above code will work fine if we replace the $string = "customers"
with $string = "CUSTOMERS"
. However, if you have a project requirement for a case-sensitive match, we can use the -CaseSensitive
parameter as follows.
1 2 3 4 5 6 7 8 9 10 |
$path = "E:\Test\file1.txt" $string = "Customers" $result = Select-String $path -Pattern $string -CaseSensitive if ($result -ne $null) { Write-Host "The specified text file contains the string '$string'." } else { Write-Host "The specified text file does not contain the string '$string'." } |
1 2 3 |
The specified text file does not contain the string 'Customers'. |
We could not find the $string
this time because we have customers
, not Customers
in file1.txt
. This happened due to the -CaseSensitive
parameter, which did a case-sensitive match and displayed the message accordingly.
For the above two code examples, -Pattern
will be treated as a regular expression due to its default behaviour, but we didn’t specify the regex but a simple string. So, in this case, we want the $string
to be interpreted as a simple string. To do that, we can use the-SimpleMatch
parameter as follows.
1 2 3 4 5 6 7 8 9 10 11 |
$path = "E:\Test\file1.txt" $string = "customers" $result = Select-String -Path $path -Pattern $string -SimpleMatch -Quiet if ($result){ Write-Host "The specified text file contains the string '$string'." } else{ Write-Host "The specified text file does not contain the string '$string'." } |
1 2 3 |
The specified text file contains the string 'customers'. |
This example is similar to the previous one. But here, we used the -SimpleMatch
parameter, which is not mandatory but using it will specify that the $string
in the pattern must not be interpreted as a regular expression.
In addition, we used the -Quiet
parameter to let the Select-String
return a Boolean value (True
/False
) instead of the MatchInfo
object having the file’s name, line number and the entire line containing the match.
Using Get-Content
Cmdlet
We can check if the file contains a string using the Get-Content
cmdlet with the Select-String
cmdlet, Contains()
method and -match
operator. Let’s learn each of them below.
Use Get-Content
with Select-String
Cmdlet
Use Get-Content
with the Select-String
cmdlet to check if the file contains the specified string in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 |
$path = "E:\Test\file1.txt" $string = "customers" $result = Get-Content $path | Select-String $string -Quiet if ($result) { Write-Host "The specified text file contains the string '$string'." } else{ Write-Host "The specified text file does not contain the string '$string'." } |
1 2 3 |
The specified text file contains the string 'customers'. |
We have already learned about the $path
, $string
, $result
variables, Select-String
cmdlet and -Quiet
parameters. The new thing we used for this example is the Get-Content
cmdlet, which was used to retrieve the content of an item at a given location; for instance, the function’s content or text in a text file.
As we are working with text files, it is essential to know how this cmdlet works with text files. First, it reads one line at a time from the given text file and returns the objects’ collection, where each denotes one line of content. After that, the Select-String
searched for the match and returned True
if the match was found. Finally, the Select-String
cmdlet returned Boolean values because we used the -Quiet
parameter that we have learned previously.
If you are using PowerShell 3.0, you can also get the
n
number of lines from the start/end of the specified item.
Use Get-Content
with the Contains()
Method
Use Get-Content
with the Contains()
method to check if the file has the specified string in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 |
$path = "E:\Test\file1.txt" $string = "customers" $fileContent = Get-Content $path if ($fileContent.Contains($string)) { Write-Host "The specified text file contains the string '$string'." } else{ Write-Host "The specified text file does not contain the string '$string'." } |
1 2 3 |
The specified text file contains the string 'customers'. |
We have already learned about the Get-Content
in the previous example; let’s extend that discussion by saying that the Get-Content
cmdlet retrieved the content of file1.txt
and returned it as a string object. So now, we can use methods that work with strings.
One of them is the Contains()
method, which was chained with $fileContent
(a string object), took the $string
as a parameter and checked if the $fileContent
contained the $string
. If it does, the $fileContent.Contains($string)
expression will return True
and cause the if
block’s execution; otherwise, the else
block will be executed.
Remember, the Contains()
method does a case-sensitive match, which means Customer
and customer
are two different values. See the following example.
1 2 3 4 5 6 7 8 9 10 11 |
$path = "E:\Test\file1.txt" $string = "Customers" $fileContent = Get-Content $path if ($fileContent.Contains($string)) { Write-Host "The specified text file contains the string '$string'." } else{ Write-Host "The specified text file does not contain the string '$string'." } |
1 2 3 |
The specified text file does not contain the string 'Customers'. |
Use Get-Content
with -match
Operator
Use Get-Content
with the -match
operator to check if the file contains the specified string in PowerShell.
1 2 3 4 5 6 7 8 9 10 |
$path = "E:\Test\file1.txt" $string = "customers" If (Get-Content $path | %{$_ -match "customers"}){ Write-Host "The specified text file contains the string '$string'." } else{ Write-Host "The specified text file does not contain the string '$string'." } |
1 2 3 |
The specified text file contains the string 'customers'. |
Here, we used the same logic but with a different operator, -match
matched every word in the text (retrieved using Get-Content
) with $string
. The expression enclosed in if
returned True
if the file1.txt
contained the $string
; otherwise, it returned False
.
The
-match
operator performs a case-insensitive match, socustomers
is the same asCustomers
.
Using Get-ChildItem
Cmdlet
The Get-ChildItem
cmdlet can also be used with Select-String
to check if the files have the specified string. Additionally, we can use this cmdlet with Select-String
and ForEach
to search an array of strings in multiple text files. Let’s see how each of these approaches works.
Use Get-ChildItem
Cmdlet with Select-String
Cmdlet
Use Get-ChildItem
with the Select-String
cmdlet to check if the file contains the specified string in PowerShell.
1 2 3 4 5 |
$path = "E:\Test\" $string = "client" Get-ChildItem -Path $path -Recurse | Select-String -Pattern $string |
1 2 3 4 |
file2.txt:1:This is file2 and has some sample text for the client. file4.txt:1:This is file2 and has some sample text for the client. |
Here, we used Get-ChildItem
with -Path
and -Recurse
parameters to retrieve all items and child items from the specified $path
. The -Path
parameter was used to mention the path while -Recurse
was used to get all items in the given $path
and all its child items. The result for Get-ChildItem -Path $path -Recurse
would be as follows:
1 2 3 4 5 6 7 8 9 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/7/2023 8:16 AM 60 file1.txt -a---- 3/7/2023 8:16 AM 57 file2.txt -a---- 3/7/2023 8:16 AM 90 file3.txt -a---- 3/7/2023 8:16 AM 57 file4.txt |
The above results were piped to the Select-String
cmdlet, which searched for the specified $string
and returned the file’s name, line number, and the entire line containing the $string
as demonstrated in the above output.
Use Get-ChildItem
with Select-String
& ForEach
To check if the files contain the string:
- Use the array operator to create an array of strings.
- After saving the path in a variable, use the
ForEach
loop to iterate over an array of strings. - In each iteration:
- Use
Get-ChildItem
to retrieve all.txt
files in the specified path. - Use the
Select-String
cmdlet to find the search string and select the path, line number, and the matched word using theSelect
cmdlet.
- Use
1 2 3 4 5 6 7 8 9 |
$searchStrings = @('customers','client') $path = "E:\Test\" Foreach ($ss in $searchStrings){ Get-Childitem -Path $path -Recurse -include "*.txt" | Select-String -Pattern "$ss" | Select Path,LineNumber,@{n='SearchWord';e={$ss}} } |
1 2 3 4 5 6 7 |
Path LineNumber SearchWord ---- ---------- ---------- E:\Test\file1.txt 1 customers E:\Test\file2.txt 1 client E:\Test\file4.txt 1 client |
As we used Select-String
to find the search string, it will perform a case-insensitive search; see the following example.
1 2 3 4 5 6 7 8 9 |
$searchStrings = @('Customers','CLIENT') $path = "E:\Test\" Foreach ($ss in $searchStrings){ Get-Childitem -Path $path -Recurse -include "*.txt" | Select-String -Pattern "$ss" | Select Path,LineNumber,@{n='SearchWord';e={$ss}} } |
1 2 3 4 5 6 7 |
Path LineNumber SearchWord ---- ---------- ---------- E:\Test\file1.txt 1 Customers E:\Test\file2.txt 1 CLIENT E:\Test\file4.txt 1 CLIENT |
That’s all about PowerShell check if file contains String.