PowerShell: calling a function with parameters

I just started with PowerShell to do some complex scripting. As a beginner in this new language I will probably run into all the quirks that the language has, but hey thats the fun with learning something new. The first quirk: calling a function with parameters.

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

f("hello", "world") # Results in: a: hello world b:
f "hello" "world"   # Results in a: hello b: world

If you put something between parentheses, it is executed as an expression first.

For more information on what you can do with functions, execute the following command in your PowerShell: Get-Help about_function

Published Wednesday, November 29, 2006 10:44 AM by svdoever
Filed under: ,

Comments

Saturday, August 18, 2007 10:07 AM by CumpsD

# re: PowerShell: calling a function with parameters

I'm sure you're going to love this sample output:

PS C:\> function f([string]$a, [string]$b)

>> {

>>   Write-Host "a:", $a, " b:", $b

>> }

>>

PS C:\> f("A", "B")

a: A B  b:

PS C:\> f "A", "B"

a: A B  b:

PS C:\> f "A" "B"

a: A  b: B

PS C:\> f("A", "B") "C"

a: A B  b: C

PS C:\>

Your behavior isn't just from the parentheses, but due to the commas.

Friday, September 14, 2007 4:22 PM by Daniel

# re: PowerShell: calling a function with parameters

PowerShell is a bit funky.. When you create a function and you want to supply parameters, the following format applies:

function fname {

  param([string]$a, [int]$b) # the two parameters.

  write-host ("a = " + $a)

  write-host ("b = " + $b)

}

Friday, February 27, 2009 1:36 PM by pdirt

# re: PowerShell: calling a function with parameters

Thanks for the clarifation

man I fought with this for a day...

I want to pass args as in c++

foo(2,20)

but in Pshell it's

foo "2" "20"

provided  that foo is written to accept params:

funtion foo {

param([int]=$arg1,[int]=$arg2)

write-host "$arg1, $arg2"

}

SO, now I have it cleared up, but still, my c++ side wants to call this like foo(2,20).

Anyway, thanks again!

Friday, July 24, 2009 4:58 PM by Christopher Scholten

# re: PowerShell: calling a function with parameters

Great, just what I was looking for. Thanks.

Tuesday, August 18, 2009 6:20 PM by Amer

# re: PowerShell: calling a function with parameters

yeah me too,i was going nuts over this !

Thursday, October 08, 2009 8:44 AM by Harsha

# re: PowerShell: calling a function with parameters

Thx,

I was fighting with PShell passing arguments, had a tuff time for half a day.

i was going mad what is wrong with it.

Anyways thanks a lot :)

Monday, November 16, 2009 4:01 PM by arenas

# re: PowerShell: calling a function with parameters

hello to all,

i don't understand all

pls i need en fonction

(or parameter) like:

function var {var1,var2,var3, ....

  if ([int64]var -lt 0)

   $var.style.color="#E30000";}

thnx for any help

AA

Thursday, December 31, 2009 11:49 AM by Hikmer

# re: PowerShell: calling a function with parameters

That sucked!  It took me 4 hours of my life to figure this out....and I cannot get them back :(

Sunday, February 07, 2010 2:58 PM by Alban

# re: PowerShell: calling a function with parameters

Thats because powershell; nice as it is; is also pointlessly and randomly obscurely different and bizarre just for the sake of it.

Its like the people who designed it had been locked away by Microsoft for forty years in a commune somewhere; isolated from the whole history of the computing universe; and then were told to create powershell untainted from anything that ever existed before.

Wednesday, February 10, 2010 2:46 AM by PRH

# re: PowerShell: calling a function with parameters

I would be probably still wondering why the parameter type inside and outside the function is different

Leave a Comment

(required) 
(required) 
(optional)
(required)