Calling PowerShell Functions with Parameters

This threw me for a loop today. You can't call a PowerShell function with commas "," or parentheses "()" and have it believe it goes to both parameters.  Try running the following and see what your output is on every line call to "foo."

function foo([string]$a, [string]$b)
{
   Write-Host "a:", $a, " b:", $b
}


foo("A", "B")
foo "A", "B"
foo "A" "B"
foo("A", "B") "C"

rm function:/foo

You may find you get different results than you may first expect. I did.

Reference: http://weblogs.asp.net/soever/archive/2006/11/29/powershell-calling-a-function-with-parameters.aspx

Print | posted @ Tuesday, August 19, 2008 7:29 PM

Comments on this entry:

Gravatar # re: Calling PowerShell Functions with Parameters
by Joe at 5/15/2009 7:25 PM

OMG seriously this shouldn't have been so difficult to figure out. Thanks for the Post.

I have found this works also (v1):

$a=1

foo $a ($a+$a)
>a:1 b:2
Comments have been closed on this topic.