The function
function findf {
Write-Host "Find files that match: $args"
gci -r | Where-Object { $_.name -match ".*$args.*" }
}
doesn’t seem to work. For example,
findf .exe
— Prints a bunch of stuff not limiting output to EXE files —
Any ideas what I’m doing wrong?
If I run the same command from the PowerShell command window the command works:
gci -r | Where-Object { $_.name -match ".*.exe.*" }
This works and correctly shows me the files that match the *.EXE pattern
$argsis an object representing a collection of arguments.You should either:
$args[0]to get the string representing the first argumentparamto define the input parameter like:.
I always advocate using
paramwhen possible, since you can strongly type your variables like I did in the example. Otherwise it might be possible to do something confusing like pass an array as a parameter which can cause all sorts of hard-to-trace issues.