How to Find a File Recursively with PowerShell

Use Get-ChildItem to search a known directory tree instead of writing a recursive folder walker. Decide whether any match is enough or whether callers need a repeatable ordering.

Find the first enumerated match

PowerShell
$searchRoot = 'C:\Work\Downloads'
$match = Get-ChildItem -LiteralPath $searchRoot -File -Recurse -Filter '*download*' -ErrorAction Stop |
    Select-Object -First 1

if ($null -eq $match) {
    'No matching file found.'
} else {
    $match.FullName
}

Replace the root with a real test directory and add a file such as daily-download.txt in a child directory. The command should print its full path. Remove or rename that test file and repeat against a tree with no matching names to exercise the empty result.

-File excludes directories. -Filter lets the filesystem provider apply the wildcard pattern during enumeration. The first result is an enumeration result, not necessarily the newest file or the alphabetically first path.

Make the selection reproducible

If alphabetical full-path order is the contract, collect and sort before selecting:

PowerShell
$match = Get-ChildItem -LiteralPath $searchRoot -File -Recurse -Filter '*download*' -ErrorAction Stop |
    Sort-Object FullName |
    Select-Object -First 1

This must enumerate the tree before it can choose the first sorted entry. For “newest,” sort by LastWriteTime descending instead and define a tie-breaker if ties matter.

Handle incomplete searches explicitly

A missing directory or an access-denied error should not silently become “not found.” This example stops on enumeration errors. If a best-effort inventory is appropriate, collect and report skipped paths separately.

Keep the search root narrow. Hidden files require an intentional -Force choice, and symbolic-link traversal requires a separate policy. Avoid recursively scanning every drive to answer a question about one application's directory.

References

Get-ChildItem (Microsoft.PowerShell.Management) - PowerShell
The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse. The Get-ChildItem cmdlet is designed to work with the items exposed by any provider. For example, items can be a file system file or directory, registry hive, or a certificate store. To list the providers available in your session, use the Get-PSProvider command. Some parameters are only available for a specific provider. For more information, see about_Providers.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem
Get-ChildItem
Sort-Object (Microsoft.PowerShell.Utility) - PowerShell
The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values. If sort properties aren't included in a command, PowerShell uses default sort properties of the first input object. If the input object's type has no default sort properties, PowerShell attempts to compare the objects themselves. For more information, see the Notes section. You can sort objects by a single property or multiple properties. Multiple properties use hash tables to sort in ascending order, descending order, or a combination of sort orders. Properties are sorted as case-sensitive or case-insensitive. Use the Unique parameter to remove duplicates from the output.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/sort-object
Sort-Object

Author: Herman Schoenfeld