Find String in File in PowerShell
Use the Select-String
cmdlet to find string in file in PowerShell. Select-String cmdlet uses regular expression to find patterns in Strings or Files. Select-String works similar to grep in Unix .
Select-String -Pattern "sample" -Path "E:\Test\File1.txt"
File1.txt:1:This is a sample file to test sample code.
The above command searched the pattern sample
from a file located in the given path. Here, the -Path
parameter is used to specify the path to search files. We can use wildcard characters while writing a directory, for instance, File1.txt
, *.doc
, or *.*
.
Note that we can specify the directory without the -Path
parameter and get the desired results. The default location is the local directory.
Select-String -Pattern "sample" "E:\Test\File1.txt"
File1.txt:1:This is a sample file to test sample code.
Remember, we will invite the error if we only specify a directory as given below.
Select-String -Pattern "sample" "E:\Test\"
Here, we used the -Pattern
parameter to specify the text to find on every line. We used the Select-String
cmdlet to find text in the given files. We can also use it to find text in files; it uses regex (regular expressions) to search and match the text patterns in the input files and strings.
Remember, the Select-String
cmdlet is based on text’s lines; by default, it looks for the first match in every line, and for every match, it shows the line number, file name, and complete text in that line which contains the match, see the above output.
We can use Select-String
to search for multiple matches in each line and shows text before and after the match. We can also print Boolean value (True
/False
), denoting whether the specified math is found or not. Finally, we can also use it to match the given pattern from all .txt
files located in the given path.
Select-String "sample" "E:\Test\*.txt"
File1.txt:1:This is a sample file to test sample code.
Use the following command to match multiple patterns per line.
Select-String "sample", "code" "E:\Test\File1.txt"
File1.txt:1:This is a sample file to test sample code.
Alternatively, we can use the above command with the array operator as given below.
Select-String @("sample", "code") "E:\Test\File1.txt"
File1.txt:1:This is a sample file to test sample code.
Further reading:
Get Length of String in PowerShell
Find Position of Character in String in PowerShell
Search String in File in directory and sub-directories in PowerShell
Use the Get-ChildItem
with Select-String
cmdlet to find the specified string in a file(s) in PowerShell.
Get-ChildItem -Path "E:\Test" -File "File1.txt" | Select-String -Pattern "sample" | Select LineNumber, Filename, Path
LineNumber Filename Path ---------- -------- ---- 1 File1.txt E:\Test\File1.txt
In the previous section, we have already learned about Select-String
, -Path
, and -Pattern
. Here, the -File
parameter was used to specify the file name which resides in the given path. Then, we used the Get-ChildItem
cmdlet to get items and child items from the given location, while Select
displayed more organized and easy-to-understand results.
Here, LineNumber
shows the line number, Filename
represents the file name, and Path
denotes the location where the specified match was found.
For example, we want to find the match from the given directory and sub-directories. In that case, we can use the -Recurse
parameter, which is used to retrieve items and child items in the given locations.
Get-ChildItem -Path "E:\Test\" -Recurse | Select-String -Pattern "sample" | Select LineNumber, Filename, Path
LineNumber Filename Path ---------- -------- ---- 1 File1.txt E:\Test\File1.txt 1 File1.txt E:\Test\FolderA\File1.txt 1 File1.txt E:\Test\FolderA\FolderD\File1.txt
We can use the -Depth
parameter to specify the depth if we do not want to go through all sub-directories.
Get-ChildItem -Path "E:\Test\" -Recurse -Depth 1| Select-String -Pattern "sample" | Select LineNumber, Filename, Path
LineNumber Filename Path ---------- -------- ---- 1 File1.txt E:\Test\File1.txt 1 File1.txt E:\Test\FolderA\File1.txt
This time, we did not get a match from FolderD
due to using the -Depth
parameter.
That’s all about how to find String in File in PowerShell.