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
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 PathThis 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 *.pdfThis 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:
- How to Check if a File Contains a String in PowerShell?
- PowerShell Write-Host to File
- How to Get File Modified Date in PowerShell?
- PowerShell Find and Replace in File
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.