Skip to main content

Recursively find all the files that match a mask, with exclusion criteria.

    Recursively find all the files that match a mask.

    Why not simply cal Get-ChildItem -Recurse ...?
    Traversing the many directories that are full of junk we're not interested
    can take a long time. Unlike 'Get-ChildItem', this function does not
    traverse directories that match the '-AntiMasks'.

    Start searching from where? Defaults to the current directory.

    A list of masks to match against the files.

    How deep should we look into subdirectories?  Default is no limit.

    Stop recursing when we reach a directory with a matching name.

    Find-Files -Masks *.txt
function Find-Files(
    $Path = $null,
    [string[]]$Masks = '*',
    $MaxDepth = -1,
    $Depth = 0,
    [string[]]$AntiMasks = @('bin', 'obj', 'packages', '.git')
) {
    foreach ($item in Get-ChildItem $Path | Sort-Object -Property Mode, Name) {
        if ($Masks | Where-Object { $item -like $_ }) {
        if ($AntiMasks | Where-Object { $item -like $_ }) {
            # Do not recurse.
        elseif ($MaxDepth -ge 0 -and $Depth -ge $MaxDepth) {
            # We have reached the max depth.  Do not recurse.
        elseif (Test-Path $item.FullName -PathType Container) {
            Find-Files $item.FullName $Masks $MaxDepth ($Depth + 1) $AntiMasks