Find Files With Extension in PowerShell

Aamir Khan Jul 25, 2022
  1. Use Get-ChildItem With Filter to Find Files With Extension in PowerShell
  2. Use Get-ChildItem With Include to Find Files With Extensions in PowerShell
Find Files With Extension in PowerShell

The tutorial uses the Get-ChildItem in PowerShell to find files with a specific extension.

Use Get-ChildItem With Filter to Find Files With Extension in PowerShell

The Get-ChildItem command in the PowerShell environment gets an item from a specified location. An item can reside in a container, and a container usually is a folder.

Moreover, the Get-ChildItem uses a -Recurse parameter to get items from the child containers or sub-containers. It also includes a -Filter parameter that uses an asterisk ( * ) wildcard to get all the files with a specific extension.

The official documentation lists all the parameters. The tutorial uses only a subset of relevant parameters.

The following command finds all the files with the .txt extension.

Get-ChildItem "C:\Files\"  -Recurse -File -Filter *.txt

Output:

Directory: C:\Files

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         5/15/2022  11:02 PM              0 file1.txt
-a----         5/15/2022  11:02 PM              0 file2.txt
-a----         5/15/2022  11:02 PM              0 file3.txt

    Directory: C:\Files\Misc files

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         5/15/2022  11:05 PM              0 file8.txt

    Directory: C:\Files\More Files

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         5/15/2022  11:02 PM              0 file4.txt

    Directory: C:\Files\More Files\Some More Files

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         5/15/2022  11:03 PM              0 file6.txt

Add the -Name parameter to the Get-ChildItem to output the file’s paths only rather than a detailed output.

Get-ChildItem "C:\Files\"  -Recurse -File -Name -Filter *.txt

Output:

file1.txt
file2.txt
file3.txt
Misc files\file8.txt
More Files\file4.txt
More Files\Some More Files\file6.txt

Use Get-ChildItem With Include to Find Files With Extensions in PowerShell

The Get-ChildItem command uses the -Include parameter that takes one or more string patterns to include the matching items.

Get-ChildItem "C:\Files\" -Recurse -File -Name -Include *.txt

Adding a trailing asterisk (*) to the file path is necessary without the - Recurse flag. In that case, it only lists the .txt files in the Files folder.

Get-ChildItem "C:\Files\*" -File -Name -Include *.txt

Output:

file1.txt
file2.txt
file3.txt

Related Article - PowerShell File