2023 Jan 9

Simple File Search Function

We might need to be able to locate files, and PowerShell might be rather helpful in that regard. Here we also use all the matches where the string is specified between two asterisks, but we can also limit it to whatever we only like.
 

The Function looks like this:

function Find-File {
    param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [string]$Name
    )
    process {
        (Get-ChildItem -Recurse -Filter $Name -ErrorAction SilentlyContinue).FullName    
    }
}

By default, it will search in the current directory and all its subdirectories, in general, it might be possible (or reasonable) to specify that one as well. The output will also skip all the warnings for the locations you don't have access to (brought to you by SilentlyContinue).

Let's see how it works on a basic example. Note, you might need to have quotes in case of a complex search queries that include some special characters or spaces. It will also accept the pipeline input if you want to search for more files:

In case you don't want to use asterisks all the time, you can simply replace -Filter $Name with something like -Filter "*$Name*" on line 7

They posted on the same topic

Trackback URL : https://dykhl.in/trackback/13

This post's comments feed