How to Create a Custom Input Box in PowerShell (With Example)


You can use the following syntax in PowerShell to create a custom input box:

Add-Type -AssemblyName Microsoft.VisualBasic

$header = 'Welcome'
$msg = 'Please enter your name:'

$user_input = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $header)

This particular example creates an input box where the title header is Welcome and the message in the box is Please enter your name.

The user can then type their response.

The text that the user types in will be stored in a variable named $user_input.

The following example shows how to use this syntax in practice.

Example: How to Create a Custom Input Box in PowerShell

Suppose that we would like to create a custom input box in PowerShell that prompts a user to type in their name.

We can use the following syntax to do so:

Add-Type -AssemblyName Microsoft.VisualBasic

$header = 'Welcome'
$msg = 'Please enter your name:'

$user_input = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $header)

The following screenshot shows how to use this syntax in practice:

Once we press Enter, the custom input box will appear where the user can type in their response:

PowerShell create custom input box

Suppose I type in Zach and then press OK.

We can then type $user_input to view the user input:

We can see that this returns Zach, which is the exact text that I typed in.

Note: You can also supply a third argument to the InputBox() function to specify a default value that should appear in the input box if you’d like.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Prompt for Input from List
PowerShell: How to Check if Input Value Exists in Array
PowerShell: How to Create an Array with Initial Size

Leave a Reply