Fancy InputBox for Mandatory Parameters

by Dec 11, 2015

When you declare a parameter mandatory and the user does not specify it, PowerShell prompts the user for it. Another way of creating mandatory parameters is to provide a default value that triggers an action.

Here is an example:

#requires -Version 2
function Get-Something
    $Name = $(
      Add-Type -AssemblyName Microsoft.VisualBasic
      [Microsoft.VisualBasic.Interaction]::InputBox('Enter Name','Name', $env:username)

  "You entered $Name."

When you run Get-Something like this, the function runs unattended:

Get-Something -Name Test

When you forget to specify -Name, a dialog box pops up, and the user can enter the value.

Twitter This Tip! ReTweet this Tip!