Recursive File Search Using PowerShell

Rohan Timalsina Jan 30, 2023
  1. Use the Get-ChildItem Cmdlet With the -Recurse Switch in PowerShell to Search Files Recursively
  2. Use dir Cmdlet With -Recurse Switch in PowerShell to Search Files Recursively
Recursive File Search Using PowerShell

Sometimes, we save a file on a computer and forget where we saved it. There are different ways to search files on the computer. One of them is PowerShell which allows you to list files and directories present in a specific location.

This tutorial will teach you to search files recursively using PowerShell.

Use the Get-ChildItem Cmdlet With the -Recurse Switch in PowerShell to Search Files Recursively

The Get-ChildItem cmdlet displays a list of files and directories on the specific location. It does not show empty directories when used with the -Recurse parameter.

For example, the following command displays the list of files and directories present in the C:\pc directory.

Get-ChildItem -Path 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

With the -Recurse parameter, you can get the files from all the directories or subdirectories from the specified locations. It means you can search the files recursively in a specific location using PowerShell.

Get-ChildItem -Path C:\pc -Filter car.png -Recurse -ErrorAction SilentlyContinue -Force

As you can see below, the car.png is found on the directory C:\pc\computing\task4. It will display all car.png files if found on multiple directories.

Output:

    Directory: C:\pc\computing\task4


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          1/3/2022   2:26 PM           3465 car.png

If you do not know the file’s name, you can search all files with the same file extension. For example, the command below will display all files having the .txt extension on the C:\pc directory.

Get-ChildItem -Path C:\pc -Filter *.txt -Recurse -ErrorAction SilentlyContinue -Force

The output displayed is usually large because it includes the Path, Mode, LastWriteTime, Length, and Name of a file. You can only display the path of a file using | %{$_.FullName}.

Get-ChildItem -Path C:\pc -Filter *.txt -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName }

Output:

C:\pc\ReadMe.txt
C:\pc\computing\task1\MatrixA.txt
C:\pc\computing\task3\password.txt

Use dir Cmdlet With -Recurse Switch in PowerShell to Search Files Recursively

The dir cmdlet is an alias for the Get-ChildItem. It also displays a list of files and directories on the specific location.

dir -Path C:\pc -Filter password.txt -Recurse

Output:

    Directory: C:\pc\computing\task3


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          1/7/2022   3:43 PM             18 password.txt
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 File