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

Thursday, February 25, 2010 11:39 AM by David

# re: PowerShell: calling a function with parameters

function fnc

 {

   param ( $str )

   $str = $str + "aaa"

   #write-host $str

   return $str

 }

cls

[string]$str = "bo"

fnc $str

write-host $str

+++++++++++++++++++++++++

I would expect $str to be "boaaa", but it still is "bo"

Tuesday, March 09, 2010 11:42 AM by Pete

# re: PowerShell: calling a function with parameters

The variable $str needs to be a reference variable of type [ref] in order for it to be called by reference.  Also need to use the .value property and the return $str is unnecessary. Try this.

function fnc

{ param ( [ref]$str )

  $str.value = $str.value + "aaa"

  #return $str

}

cls

[string]$str = "bo"

fnc ([ref]$str)

write-host $str

Tuesday, March 23, 2010 11:17 AM by Jeovane Maciel

# re: PowerShell: calling a function with parameters

Important missing point: You simply can't call the function before you had written down that into the script, for instance:

this does works:

function foo(){Write-Host "foo"}

foo()

this doesn't works:

foo()

function foo(){Write-Host "foo"}

Knowing this may save some time, don't you think?

Thursday, May 20, 2010 9:47 AM by Taylor

# re: PowerShell: calling a function with parameters

How can you call a powershell function that has parameters that is in a different script??

Thanks for the help!

Sunday, May 23, 2010 9:25 PM by MusicAndCode

# re: PowerShell: calling a function with parameters

Thanks a lot for the comment on how to call a function. I'm a couple hours old in powershell and I'm quite surprised by the some of the syntax.

HaGD

Phil

Wednesday, May 26, 2010 8:23 PM by Parag

# re: PowerShell: calling a function with parameters

Thanks a lot.This really help me from tearing half of my hair out.the other half I tore up already trying to pass the arguments in the function.

Wednesday, June 16, 2010 9:01 AM by mitchellm44

# re: PowerShell: calling a function with parameters

This helped - the C# mindeset kept thinking put parens and then args.

so now it is something like

function fname {

 param(

[string]parmOne=$(throw "parmOne required"),

[int]parm2

)## End of Parms and start of function

<statments of function>

} #end brace of function

I tried to pass 2 args with and without parens and it was not working.

fname -parmOne "Data" -parm2 256

I had to pass the parameters as NAMED variables. And then it worked.

Thanks - your post was the first in Google results.

Monday, May 09, 2011 4:14 PM by ajc

# re: PowerShell: calling a function with parameters

How about some how-to on passing a boolean (i.e. Switch-type) param into a .ps1 script file?

Monday, May 30, 2011 8:20 PM by Kirill

# re: PowerShell: calling a function with parameters

Thanks!

I spent more than 2h trying to sort this out.

Kirill

Friday, June 17, 2011 10:59 AM by Elmar

# re: PowerShell: calling a function with parameters

Thanks a lot!

really strange behavior !!

@ajc (and others try passing boolean params):

the type should be "bool"

   Param(

             [string] $VM,

             [bool] $Suspend

         )

this works

Monday, September 05, 2011 4:25 PM by hooher tod

# re: PowerShell: calling a function with parameters

Yes there should realize the reader to RSS my feed to RSS commentary, quite simply

Monday, September 26, 2011 6:26 AM by Abdul

# re: PowerShell: calling a function with parameters

Thanks for the concepts of parameter passing with functions.

Wednesday, November 09, 2011 3:45 PM by ctwovaat

# re: PowerShell: calling a function with parameters

man .. thanks so much

Leave a Comment

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