How to Search a String in Multiple Files and Return the Name of Files in PowerShell

Rohan Timalsina Feb 02, 2024
  1. Use Get-ChildItem and Select-String Cmdlet to Search a String in Multiple Files and Return the Name of Files in PowerShell
  2. Use ls and sls to Search a String in Multiple Files and Return the Name of Files in PowerShell
How to Search a String in Multiple Files and Return the Name of Files in PowerShell

You can view the files present in a specific directory using PowerShell. It also allows you to search files recursively to find the files present in the sub-directories.

But is it possible to search a string in files and get the names of those files in PowerShell? The answer is yes. This tutorial will teach you to search a string in multiple files and return the name of files in PowerShell.

Use Get-ChildItem and Select-String Cmdlet to Search a String in Multiple Files and Return the Name of Files in PowerShell

The Get-ChildItem cmdlet displays a list of files and directories present in the specific location. -Recurse parameter helps list all the files, directories, or sub-directories recursively. But it does not show empty directories in the output.

The Select-String cmdlet searches and finds string patterns in files.

The following command will search a string system in all files on the C:\pc directory and lists a file’s complete path.

Get-ChildItem -Path C:\New -Recurse | Select-String -Pattern 'system' -List | select Path

Output:

Path
----
C:\New\literature_review.pdf
C:\New\Proposal.pdf
C:\New\Draft.pdf
C:\New\Report.docx
C:\New\fyp\Forms.docx

You can also display the line containing the string if you include Line in the command.

Get-ChildItem -Path C:\New -Recurse | Select-String "system" -List | select Path, Line

Use ls and sls to Search a String in Multiple Files and Return the Name of Files in PowerShell

The ls command is popular to list files and directories in Unix and Unix-like operating systems. The ls command is also available in the PowerShell and functions similarly.

If the path is not specified, it lists the files and directories present in the working directory. With the -r option, ls lists the files and directories recursively.

ls C:\pc

Output:

    Directory: C:\pc


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          1/2/2022   2:53 PM                computing
d-----          1/2/2022   1:24 PM                New folder
-a----          1/2/2022   1:36 PM          17384 hello
-a----          1/2/2022   2:48 PM           5134 matrix.c
-a----        12/26/2020   7:03 PM            321 ReadMe.txt

The sls is an alias for the Select-String cmdlet. It also searches for a string pattern in files.

You can use the command below to search a string in multiple files and get the name of files in PowerShell.

ls -r -Path C:\pc | sls 'hello' | select -u Path

Output:

Path
----
C:\pc\computing\task3\crack
C:\pc\computing\task3\crack1
C:\pc\computing\task3\crack2
C:\pc\computing\task3\cracked
C:\pc\computing\task3\pass
C:\pc\computing\task3\password.txt
C:\pc\computing\task3\pwd
C:\pc\computing\task4\hello
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell String