Get-ChildItem with Variable Filter

It really shouldn't be this hard. Really it shouldn't. I've read the examples but I'm running short on ideas to resolve this.

Why won't this WORK!

Function Get_ToolData($path,$filter){
$files = Get-ChildItem $path -Filter $filter
$files
}

Get_ToolData("\\server\path","*.csv")

September 14th, 2015 10:30am

Do not use parentheses and commas when calling a PowerShell function.

The way you are calling your function, you are passing an array to your function's $path parameter.

Your function call should be:

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 10:33am

That was it. 

I got my JavaScript mixed with my PowerShell.

Thank you!

September 14th, 2015 10:35am

It is inconsistent in PowerShell that you can code the formal arguments to a function with parentheses and commas, yet you cannot call the function using that pattern.   It is just a PowerShell thing that has to be learned and reinforced before it becomes commonplace to the user of PowertShell.   The confusing thing is that
 ("\\server\path","*.csv")
is interpreted as a value that is mapped only to $path and $filter is allowed to not have a value, with no runtime diagnostics to warn about that seldom desired pattern when a function argument is expected to be left undefined at times.   Someday, I'd like to see a "lint" like level of diagnostics built into PowerShell.     lint was the name originally given to a program that flagged suspicious and non-portable constructs (likely to be bugs) in C language source code.

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 12:15pm

This is because, in PowerShell, the param statement can be included in the function header:

September 14th, 2015 12:22pm

This is because, in PowerShell, the param statement can be included in the function header:

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 2:35pm

I thought about that, but look at the param(,,,) pattern and you see the same use of parenthesis and commas as the pattern I wrote about.

That is true, but param is a single statement with an array of declared parameters. That's not the same as passing parameters to a script or function. Understandably, this is still a bit confusing, but in my opinion it is slightly less confusing than declaring the parameters directly after the function name.

September 14th, 2015 2:51pm

I liked using PARAM instead of (item1,item2) method.  This way I can user the parameters in any order.  It's just easier.

Thanks for the thoughts on this.  Much appreciated.

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 2:53pm

The parameter order stays the same no matter whether you declare them using the param statement or directly after the function name.

In either case, though, you can call the function using parameters in whatever order you want if you include the parameter names.

This is documented in the help topics about_Parameters and about_Command_Syntax.

September 14th, 2015 3:03pm

The parameter order stays the same no matter whether you declare them using the param statement or directly after the function name.

In either case, though, you can call the function using parameters in whatever order you want if you include the parameter names.

This is documented in the help topics about_Parameters and about_Command_Syntax.

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 3:37pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics