How to Find Files With Extension in PowerShell
-
Use
Get-ChildItem
WithFilter
to Find Files With Extension in PowerShell -
Use
Get-ChildItem
WithInclude
to Find Files With Extensions in PowerShell - Conclusion
PowerShell is a versatile and powerful tool for managing files and directories on your computer. One common task is finding files with specific extensions.
In this article, we’ll explore the methods to efficiently locate files with particular extensions using the Get-ChildItem
cmdlet in PowerShell.
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.
Syntax:
Get-ChildItem -Path "File Path" -Recurse -File -Filter *.txt
Parameters:
-Path "File Path"
: Specifies the search’s starting directory, where we’ll look for specific extensions.-Recurse
: Extends the search to subdirectories, providing a comprehensive search.-File
: Focuses on files, not directories, ensuring only files are returned.-Filter *.txt
: Sets the filter for files with the".txt"
extension. The asterisk (*
) matches any characters before".txt"
.
Let’s give an example. The following command finds all the files with the .txt
extension.
Get-ChildItem "C:\Files\" -Recurse -File -Filter *.txt
In the command, we begin our search by using the Get-ChildItem
cmdlet on the "C:\Files"
directory, which enables us to collect information about items, whether they are files or directories, within that location. To extend our search to all subdirectories as well, we include the -Recurse
parameter.
Then, to specify that we’re interested in files and not directories, we add the -File
parameter. Finally, we define our filter condition using -Filter *.txt
, which looks for files ending with ".txt"
with the asterisk (*
) serving as a wildcard character to match any characters that precede it.
This way, we effectively instruct PowerShell to locate all files with names concluding in ".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
We could 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
We can see, in the output, that it only shows the file’s names and paths after executing the command with the -Name
parameter.
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.
Syntax:
Get-ChildItem "File Path" -Recurse -File -Name -Include *.txt
-Path "File Path"
: Specifies the starting directory for the search, where we seek files of a specific extension.-Recurse
: Extends the search to all subdirectories, ensuring a comprehensive search.-File
: Focuses exclusively on files, excluding directories from the results.-Name
: Lists only file names in the output without detailed file information.-Include *.txt
: Filters and retrieves files with the".txt"
extension in the results.
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
In the command, we use the Get-ChildItem
cmdlet to initiate a search for files within the "C:\Files"
directory and all its subdirectories. To pinpoint files with a ".txt"
extension, we apply a series of parameters.
We start by setting the path with "C:\Files\*"
, which includes all subdirectories under "C:\Files"
. By adding the -File
parameter, we concentrate solely on files, excluding directories.
The -Name
parameter refines our output, displaying only the names of the files, while the -Include *.txt
parameter fine-tunes the search by filtering in files with the ".txt"
extension.
Output:
file1.txt
file2.txt
file3.txt
The output represents a list of file names with the ".txt"
extension, which matches the criteria we specified in our PowerShell command.
Conclusion
In this article, we’ve explored efficient methods for using the Get-ChildItem
cmdlet in PowerShell to locate such files. We’ve covered two approaches: using the -Filter
parameter for precision and the -Include
parameter for flexibility.
These techniques can simplify tasks like data analysis and organization, making file management in a Windows environment more efficient.