When working with PowerShell, there are several occasions when you need to prompt the user for input. This is, in fact, a very common requirement among PowerShell developers. In this tutorial, I will explain various methods to prompt for user input in PowerShell with examples.
To prompt for user input in PowerShell, you can use the Read-Host cmdlet, which reads a line of input from the console. The syntax is straightforward: $variable = Read-Host “Prompt message”. For example, you can prompt the user for their first name with $firstName = Read-Host “Please enter your first name”, and then use the input in your script.
Prompt for Input in PowerShell
A few scenarios where you need to prompt for input in PowerShell are gathering user credentials, asking for a file path, or simply requesting a yes/no confirmation, etc.
PowerShell provides different methods for prompting users for input. Let me show you each method with an example.
Method 1: Using Read-Host
The most used way to prompt for input in PowerShell is by using the Read-Host cmdlet. This cmdlet reads a line of input from the console.
Syntax:
Here is the syntax:
$variable = Read-Host "Prompt message"Example:
Let me show you a few examples.
$firstName = Read-Host "Please enter your first name"
$lastName = Read-Host "Please enter your last name"
Write-Output "Hello, $firstName $lastName!"In this example, the script prompts the user to enter their first and last names and then greets them using the provided information.
I executed the above script using VS code, and you can see the exact output in the screenshot below:

Check out Add Credentials in PowerShell Script
Method 2: Using Get-Credential
When you need to securely prompt for a username and password, Get-Credential is the go-to cmdlet in PowerShell. It provides a standard dialog box for user credentials.
Syntax:
Here is the syntax:
$credential = Get-CredentialExample:
Let me show you an example.
$credential = Get-Credential -Message "Please enter your credentials for the secure server"
Write-Output "Username: $($credential.UserName)"This example prompts the user to enter their credentials and then displays the username they entered.
Here is the exact output in the screenshot below:

Check out Add Comments in PowerShell
Method 3: Using [System.Windows.Forms]
For more advanced input forms, you can use .NET’s Windows Forms. This method is more complex but offers greater flexibility.
Syntax:
Here is the syntax:
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
# Additional form setup code
$form.ShowDialog()Example:
Here is an example.
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = "User Input Form"
$form.Size = New-Object System.Drawing.Size(300,200)
$label = New-Object System.Windows.Forms.Label
$label.Text = "Enter your city:"
$label.Location = New-Object System.Drawing.Point(10,20)
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,50)
$form.Controls.Add($textBox)
$button = New-Object System.Windows.Forms.Button
$button.Text = "Submit"
$button.Location = New-Object System.Drawing.Point(10,80)
$button.Add_Click({
$form.Close()
})
$form.Controls.Add($button)
$form.ShowDialog()
$city = $textBox.Text
Write-Output "You entered: $city"This example creates a simple form with a label, textbox, and button, allowing users to input their city.
Check PowerShell Where-Object
Method 4: Using Out-GridView
For scenarios where users need to select from a list, Out-GridView is an excellent choice. It displays a grid view window that allows users to select items.
Syntax:
Here is the syntax:
$selection = $data | Out-GridView -PassThru -Title "Select an item"Example:
Here is an example.
$states = @("California", "Texas", "New York", "Florida")
$selectedState = $states | Out-GridView -PassThru -Title "Select your state"
Write-Output "You selected: $selectedState"This example prompts the user to select a state from a list and then displays the selected state.
Here is the output you can see in the screenshot below:

Check out PowerShell Prompt for Input Yes/No
PowerShell Prompt for Input with Default
In PowerShell, prompting for input with a default value can be useful for providing a suggested input while allowing the user to enter their own value. Here, I’ll explain two methods to achieve this: using Read-Host with a default value and using a custom prompt with a default value.
Method 1: Using Read-Host with a Default Value
While Read-Host does not directly support default values; you can achieve this by displaying the default value in the prompt message and then handling the user’s input accordingly.
Example:
Let me provide an example.
$defaultCity = "New York"
$city = Read-Host "Please enter your city (default: $defaultCity)"
if ([string]::IsNullOrWhiteSpace($city)) {
$city = $defaultCity
}
Write-Output "You entered: $city"In this example, the script prompts the user to enter their city, showing “New York” as the default value. If the user presses Enter without typing anything, the script assigns “New York” as the city.
I executed the above script, and you can see the exact output in the screenshot below:

Method 2: Using a Custom Prompt with Default Value
You can create a custom prompt function that includes a default value and handles the input accordingly.
Example:
Now, let me show an example.
function Prompt-WithDefault {
param (
[string]$Message,
[string]$DefaultValue
)
$input = Read-Host "$Message (default: $DefaultValue)"
if ([string]::IsNullOrWhiteSpace($input)) {
return $DefaultValue
} else {
return $input
}
}
$city = Prompt-WithDefault -Message "Please enter your city" -DefaultValue "Chicago"
Write-Output "You entered: $city"In this example, the Prompt-WithDefault function takes a message and a default value as parameters. It prompts the user with the message and default value; if the user does not provide any input, the function returns the default value.
Conclusion
In this tutorial, I explained how to prompt for user input in PowerShell. I recommended using the Read-Host PowerShell cmdlet for this, but you can also use the other methods I explained.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.