How to Find a Specific File by Name Using PowerShell
This article demonstrates how we can locate a specific file with PowerShell.
The Get-ChildItem
cmdlet is a useful PowerShell utility we can employ to locate a file by name or extension. It makes it easier and faster to locate a file than searching on Windows Explorer.
Find a Specific File by Name Using PowerShell Get-ChildItem
We can use the Get-ChildItem
cmdlet in PowerShell to show a list of files and folders in one or multiple locations. We can add a search pattern to narrow our output to a specific file.
Let’s check out a few examples.
Assuming we want to locate all the files in the current folder that have a .txt
extension, how do we go about it?
We will run the Get-ChildItem
command as shown below:
PS C:\Users> Get-ChildItem *.txt
The command above will locate all the files in the Users
directory with the .txt
extension. What if we want to display all the files in the root of our C:\
drive?
PS C:\pc\Users> Get-ChildItem -Path C:\
In the command above, the Git-ChildItem
cmdlet takes the C:\
as the -Path
parameter and displays all files and directories stored in the location.
We can add the -Recurse
parameter to find and list all the files in the drive C:\
location. The command will throw an error if there are files present that the user does not have access to.
To continue with the operation in case of an error, we run the command as illustrated below:
PS C:\pc\Users> Get-ChildItem -Path C:\ -Recurse -ErrorAction SilentlyContinue
We can also specify a pattern to exclude. Here is an example:
PS C:\pc\Users> Get-ChildItem -Exclude *.exe -Recurse
The above command will list all files in subdirectories present in the Users
directory and exclude the files with a .exe
file extension.
We can also use the -Filter
parameter to narrow our search to a specific file extension. Check out the example below:
PS C:\pc\Users> Get-ChildItem -Filter *.txt -Recurse
The command above will only list the files with a .txt
extension. What if we want to search for a specific file name?
We will run the Get-ChildItem
command as shown below:
PS C:\pc\Users> Get-ChildItem -recurse -filter "Trial" -File
The command above will recursively search for files with file names that match Insert
in the current folder. We can specify a folder using the -Path
parameter as illustrated below:
PS C:\pc\Users> Get-ChildItem -recurse -filter "Trial" -File -Path C:\
The command above will recursively search for files with file names that match Insert
in our C:\
drive. You can use the gci
alias instead of typing Get-ChildItem
in full.
Alternatively, we can run the following:
PS C:\pc\Users> Get-ChildItem -Recurse | where { $_.Name -match 'Insert' } | select Fullname
In the command above, the Get-ChildItem
cmdlet will recursively find files in our current folder. The Where-Object
cmdlet will compare the file name property that matches Insert
and output the file’s full name.
In a nutshell, we can use the Get-ChildItem
cmdlet to find specific files on PowerShell.
There are various parameters we can employ to narrow down our search. Using PowerShell is much easier and faster than searching on Windows Explorer.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn