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
$args is an object representing a collection of arguments.
You should either:
$args[0] to get the string representing the first argumentparam to define the input parameter like:.
function findf {
param ([string]$SeachStr)
Write-Host "Find files that match: $SeachStr"
gci -r | Where-Object { $_.name -match ".*$SeachStr.*" }
}
I always advocate using param when 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With