How to Find File by Name in PowerShell?

One of my team members got a requirement to find files by name using PowerShell while working on an automation project. In this tutorial, I will show you different methods to find files by name in PowerShell.

To find a file by name using PowerShell, you can utilize the Get-ChildItem cmdlet combined with the -Name parameter for a basic search, or add -Recurse to search through all subdirectories. For example, Get-ChildItem -Recurse -Name “myDocument.txt” will search for a file named “myDocument.txt” throughout the file system. Use wildcards with -Filter for broader searches, such as Get-ChildItem -Recurse -Filter “*.txt” to find all text files.

Find Files by Name in PowerShell

There are various PowerShell cmdlets for searching files by name. Let’s check each method with a complete script and examples.

1. Using Get-ChildItem

PowerShell provides the Get-ChildItem cmdlet to search for files by name in a directory. It can search through directories and list all files and folders that match the specified criteria.

Example 1: Basic Search

To find a file by name in the current directory using PowerShell, you can use the following command:

Get-ChildItem -Name "MyFile.txt"

Example 2: Recursive Search

To search for a file recursively through all subdirectories, you can add the -Recurse parameter with the Get-ChildItem cmdlet:

Get-ChildItem -Recurse -Name "MyFile.txt"

Example 3: Using Wildcards

If you’re unsure about the exact name or want to find files with similar names, you can use wildcards:

Get-ChildItem -Recurse -Name "*partialname*"

Example 4: Filtering by Extension

To find all files with a specific extension using PowerShell, you can filter the results:

Get-ChildItem -Recurse -Filter "*.txt"

2. Using Get-Item

The Get-Item cmdlet is another way to find a file by name using PowerShell. It is generally used when you know the exact path of the file.

Here is an example.

Get-Item "C:\Path\To\Your\File\filename.txt"

3. Using Select-String

For searching inside files for specific content and returning the file names that contain that content, Select-String is your cmdlet.

Here is the complete PowerShell script.

Get-ChildItem -Recurse | Select-String -Pattern "searchTerm" | Select-Object -Unique Path

4. Advanced Filter using Where-Object

You can also find files by name using the where-object in PowerShell.

Here is an example to get a particular file by name.

Get-ChildItem -Recurse | Where-Object { $_.Name -eq "filename.txt" }

If you want to find only .txt files, then run the below PowerShell script.

Get-ChildItem -Recurse | Where-Object { $_.Extension -eq ".txt" }

Error Handling While Searching Files

When searching for files, you may encounter errors. You can handle them using try-catch blocks in the PowerShell script.

Here is an example:

try {
    Get-ChildItem -Path "C:\Invalid\Path" -ErrorAction Stop
} catch {
    Write-Error "An error occurred: $_"

For repetitive tasks, you can create a custom function to find files by name. Here is a custom PowerShell function that you can reuse.

function Find-FileName {
    param (
    Get-ChildItem -Recurse -Name $FileName


In this PowerShell tutorial, I have explained different methods to find files by name in PowerShell. One of the easiest methods to achieve this is by using the Get-ChildItem PowerShell cmdlet.

You may also like: