Prompting for Function Parameters

by Jun 1, 2015

With a simple trick, you can add a dialog window that helps users to provide the function parameters for your function.

Simply use $PSBoundParameters to determine if a user submitted parameters. If not, run Show-Command and submit the name of your function, then return your function without doing anything.

Show-Command automatically takes care of the rest: it displays a dialog which prompts for all function parameters, and once the user clicks "Run", it runs the function with the submitted arguments.

function Show-PromptInfo
{
  param
  (
    [string]
    $Name,
    
    [int]
    $ID
  )
  if ($PSBoundParameters.Count -eq 0)
  {
    Show-Command -Name Show-PromptInfo
    return
  }
  
  "Your name is $name, and the id is $id."
}

When you run the function Show-PromptInfo with parameters, it immediately executes your call.

 
PS> Show-PromptInfo -Name weltner -ID 12
Your name is weltner, and the id is 12.

PS> Show-PromptInfo 
<# Dialog opens, then runs the function with submitted parameters#>
 

When you run the function without any parameters, a dialog opens and prompts interactively for your parameters.

Twitter This Tip! ReTweet this Tip!