Welcome to MSDN Blogs Sign in | Join | Help

Checking for bound parameters

I recently read an email where someone was asking how they could check in their function if a value had been provided to one of the parameters.  Consider for example,

 

001
002
003
004
005
006
007
008

function foo
{
  param(
    $x
  )
    
  #If $x was given a value do something, else do something else.
}

 

The usual way I’ve seen people deal this problem is by providing a default value which gets assigned to $x in the case that a value wasn’t provided by the caller and then checking for that value. 

 

001
002
003
004
005
006
007
008

function foo
{
  param(
    $x = $null
  )
  
  # Check for $x being null.
}

 

That works in the majority of cases, but let’s say that you wanted to differentiate between a default $null value and $null being passed by the caller.  What would you do?

If you’re on v2, a simple solution is to look at $PSBoundParameters.  It’s a hashtable containing the parameters that were bound and what values were bound to them.  Try this:

 

 

001
002
003
004
005
006
007
008
009

function foo
{
  param(
    $x,
    $y
  )
  
  $PSBoundParameters
}

 

PS >foo 1

 

Key                                                                       Value

---                                                                       -----

x                                                                             1

 

 

PS >foo 1 'foo'

 

Key                                                                       Value

---                                                                       -----

x                                                                             1

y                                                                           foo

 

 

 

With $PSBoundParameters containing all the bound parameters, to check if a parameter was given a value, it’s a simple matter of checking if $PSBoundParameters contains a key for it.

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015



function foo
{
  param(
    $x
  )
  
  if ($PSBoundParameters.ContainsKey('x'))
  {
    write-host 'X Bound!'
  }
}

 

 

- Marcel Ortiz Soto [MSFT]

 

 

Published Monday, April 06, 2009 7:56 PM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Checking for bound parameters

What does it occur in the following case?

---

function foo

{

 param(

   $x=10

 )

 if ($PSBoundParameters.ContainsKey('x'))

 {

   write-host 'X Bound!'

 }

}

foo

---

How to know if $X has a default value ?

Thursday, April 09, 2009 12:12 PM by Laurent Dardenne

# Microcode: PowerShell Scripting Trick: Fun With Parameter Binding: The Fake Parameter Set Trick

I’ve been hearing a lot of questions recently about what things you can do with advanced functions. 

Friday, April 10, 2009 3:00 AM by Media And Microcode

# re: Checking for bound parameters

function foo

{

 param(

   $x, $y

 )

 if ($PSBoundParameters.ContainsKey('x'))

 {

   write-host 'X Bound!'

 }

 if ($PSBoundParameters.ContainsKey('y'))

{

   write-host 'Y Bound!'

 }

Write-Host " Bound Parameters  "  $PSBoundParameters -f RED

}

Thanx.

Monday, June 22, 2009 11:29 PM by Andrew Tearle

# re: Checking for bound parameters

Is PSBoundParameters available in compiled cmdlets?

Wednesday, July 29, 2009 9:13 PM by Jack Straw

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker