How to search for files recursively in PowerShell?

Are you trying to search files inside a directory and sub-directories? PowerShell made it easy. In this tutorial, I will explain to you how to search for files recursively in PowerShell.

To search for files recursively in PowerShell, use the Get-ChildItem cmdlet with the -Recurse parameter. For example, to find all .txt files in a folder and its subfolders, use Get-ChildItem -Path “C:\YourFolder” -Recurse -Filter *.txt. This command will list all .txt files within the specified directory, traversing all levels of subdirectories.

Search for files recursively in PowerShell

Now, let me show you how to search files recursively in PowerShell using different methods.

Method 1: Using Get-ChildItem

The Get-ChildItem cmdlet is the go-to command for listing items in one or more specified locations. When combined with the -Recurse parameter, it can search through directories and their subdirectories. Here’s a basic example of how to use Get-ChildItem to find all files with a .txt extension recursively:

Get-ChildItem -Path C:\MyFolder -Recurse -Filter *.txt
Search for files recursively in PowerShell

This command will list all text files in ‘MyFolder’ and all its subfolders. However, if you want to add more conditions, such as searching for files modified in the last 7 days, you can pipe the results to the Where-Object cmdlet:

Get-ChildItem -Path C:\MyFolder -Recurse -Filter *.txt | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) }

Method 2: Utilizing the -Include and -Exclude Parameters

To refine your search, Get-ChildItem also supports the -Include and -Exclude parameters. These parameters allow you to specify patterns to include or exclude from the search results. For instance, to search for all .log files while excluding any that contain ‘archive’ in the filename, you could use:

Get-ChildItem -Path C:\Logs -Recurse -Include *.log -Exclude *archive*

This command will recursively search for log files in the ‘Logs’ directory, omitting those with ‘archive’ in their name.

Method 3: Combine with Select-String for Content Searches

Sometimes, you need to search for files containing specific text. This is where Select-String comes into play, allowing you to match strings inside file content. Here’s an example script that searches for files containing the word ‘error’:

Get-ChildItem -Path C:\Logs -Recurse -File | Select-String -Pattern "error" | Select-Object -Unique Path

This script lists files in ‘Logs’ recursively, scans each file for the word ‘error’, and then outputs each file path only once, regardless of how many matches there are.

Method 4: Advanced Filtering with Script Blocks

For more complex searches, you can use script blocks to create advanced filters. This approach provides a high level of control over the search criteria. Below is a script that finds all files larger than 1MB in PowerShell:

Get-ChildItem -Path C:\Data -Recurse | Where-Object { $_.Length -gt 1MB }

This command will return files from ‘Data’ that exceed 1MB in size.

Method 5: Using the Resolve-Path Cmdlet for Pattern Expansion

If you’re dealing with complex path patterns, Resolve-Path can help expand them before performing a search. Here’s how you can use it in conjunction with Get-ChildItem:

Resolve-Path "C:\Projects\*\*.config" | ForEach-Object { Get-ChildItem -Path $_.Path -Recurse }

This script expands the wildcard pattern to find all .config files within any subdirectory of ‘Projects’.

Method 6: Create Custom Functions for Reusability

If you want to reuse this, you can create a custom function in PowerShell. Below is a basic function that encapsulates the search logic for reuse:

function Find-FilesRecursively {
    param(
        [string]$Path,
        [string]$Pattern
    )
    Get-ChildItem -Path $Path -Recurse -Filter $Pattern
}

# Usage
Find-FilesRecursively -Path C:\Documents -Pattern *.pdf

This function can be added to your PowerShell profile for quick access and can be called with different paths and patterns as needed.

Conclusion

I hope now you have learned how to search for files recursively in PowerShell using various methods.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.