Updated June 28, 2023
Introduction to PowerShell prompt for input
In PowerShell, users can retrieve the input by prompting them with Read-Host Cmdlet. It acts as a stdin and reads the input supplied by the user from the console. Since the input can also be stored as a secured string, passwords can be prompted using this cmdlet. In normal PowerShell or ISE, a colon is displayed at the end of the prompt requesting input in a more GUI-enhanced ISE; a pop-up is displayed along with a few buttons. This article will explain in detail about getting user input in PowerShell using the prompt.
Syntax:
Read-Host
NAME
Read-Host
Syntax:
Read-Host [[-Prompt] <Object>] [-AsSecureString] [<CommonParameters>]
ALIASES
None
Parameters:
-AsSecureString:
This indicated that the input typed by the user is hidden with *. When this parameter is used, the output is a specific string object. The data type of this parameter is Switch. The default value is none. Both pipeline input and wild card characters are not accepted.
-MaskInput:
This is like the secure string parameter in functionality, except that the output returned by this is a string and is not a secure string. The data type of this parameter is Switch. The default value is none. Both pipeline input and wild card characters are not accepted.
-Prompt:
This denotes the prompt text that should be displayed to the user. This needs to be a string. In the case of spaces, they should be enclosed within quotes. The data type of this parameter is an object. The default value is none. Both pipeline input and wild card characters are not accepted.
Examples of PowerShell prompts for input
Example #1: Normal Prompt
Input:
Write-Host "Welcome to demo of powershell prompt input" -ForegroundColor Green
$name= Read-Host -Prompt "Enter your name"
$age= Read-Host -Prompt "Enter your age"
$city= Read-Host -Prompt "Enter your city"
Write-Host "The entered name is" $name -ForegroundColor Green
Write-Host "The entered age is" $age -ForegroundColor Green
Write-Host "The entered city is" $city -ForegroundColor Green
Output:
Example #2: Saving the input as a secure string
Input:
Write-Host "Welcome to demo of powershell prompt input" -ForegroundColor Green
$s1= Read-Host -Prompt "Enter your subject 1 name" -AsSecureString
$s2= Read-Host -Prompt "Enter your subject 2 name" -AsSecureString
$s3= Read-Host -Prompt "Enter your subject 3 name" -AsSecureString
Write-Host "The entered name is" $s1 -ForegroundColor Green
Write-Host "The entered age is" $s2 -ForegroundColor Green
Write-Host "The entered city is" $s3 -ForegroundColor Green
Output:
Example #3:Custom form
Input:
Write-Host "Demo of custom prompt using form" -ForegroundColor Green
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$testform = New-Object System.Windows.Forms.Form
$testform.Text = 'Data Entry Form'
$testform.Size = New-Object System.Drawing.Size(400,300)
$testform.StartPosition = 'CenterScreen'
$okb = New-Object System.Windows.Forms.Button
$okb.Location = New-Object System.Drawing.Point(85,130)
$okb.Size = New-Object System.Drawing.Size(75,25)
$okb.Text = 'Add'
$okb.DialogResult = [System.Windows.Forms.DialogResult]::OK
$testform.AcceptButton = $okb
$testform.Controls.Add($okb)
$cb = New-Object System.Windows.Forms.Button
$cb.Location = New-Object System.Drawing.Point(170,130)
$cb.Size = New-Object System.Drawing.Size(75,25)
$cb.Text = 'Remove'
$cb.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$testform.CancelButton = $cb
$testform.Controls.Add($cb)
$test = New-Object System.Windows.Forms.Button
$test.Location = New-Object System.Drawing.Point(270,130)
$test.Size = New-Object System.Drawing.Size(75,25)
$test.Text = 'close'
$test.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$testform.AcceptButton = $test
$testform.Controls.Add($test)
$lb = New-Object System.Windows.Forms.Label
$lb.Location = New-Object System.Drawing.Point(20,40)
$lb.Size = New-Object System.Drawing.Size(240,20)
$lb.Text = 'Please enter the information in text box:'
$testform.Controls.Add($lb)
$tb = New-Object System.Windows.Forms.TextBox
$tb.Location = New-Object System.Drawing.Point(40,80)
$tb.Size = New-Object System.Drawing.Size(240,20)
$testform.Controls.Add($tb)
$testform.Topmost = $true
$testform.Add_Shown({$tb.Select()})
$rs = $testform.ShowDialog()
if ($rs -eq [System.Windows.Forms.DialogResult]::OK)
{
$y = $tb.Text
Write-Host "Entered text is" -ForegroundColor Green
$y
}
Output:
Example #4: Prompting for input from the user using a dialog box
Input:
$yeah = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$nah = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$abort = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yeah, $nah, $abort)
$heading = "Demo"
$mess = "are you sure you want to continue?"
$rslt = $host.ui.PromptForChoice($heading, $mess, $options, 1)
switch ($rslt) {
0{
Write-Host "Yes" -ForegroundColor Green
}1{
Write-Host "No" -ForegroundColor Red
}2{
Write-Host "Cancel" -ForegroundColor Red
}
}
$mess = "are you satisfied with out service?"
$rslt = $host.ui.PromptForChoice($heading, $mess, $options, 1)
switch ($rslt) {
0{
Write-Host "Yes" -ForegroundColor Green
}1{
Write-Host "No" -ForegroundColor Red
}2{
Write-Host "Cancel" -ForegroundColor Red
}
}
$mess = "are you sure you want to exit?"
$rslt = $host.ui.PromptForChoice($heading, $mess, $options, 1)
switch ($rslt) {
0{
Write-Host "Yes" -ForegroundColor Green
}1{
Write-Host "No" -ForegroundColor Red
}2{
Write-Host "Cancel" -ForegroundColor Red
}
}
Output:
Example #5
Input:
Write-Host "Demo of getting confirmation along with prompt from user"
$question1 = Read-Host "do you want to continue"
if ($question1 -eq 'y') {
Write-Host "answer provided is yes" -ForegroundColor Green
}
else
{
Write-Host "answer provided is no" -ForegroundColor Red
}
$question2 = Read-Host "are you a human"
if ($question2 -eq 'y') {
Write-Host "yes, human" -ForegroundColor Green
}
else
{
Write-Host "not a human" -ForegroundColor Red
}
$question33 = Read-Host "do you believe in god"
if ($question1 -eq 'y') {
Write-Host "yes I believe in god" -ForegroundColor Green
}
else
{
Write-Host "no I dont believe in god" -ForegroundColor Red
}
Output:
Example #6
Getting multiple inputs from users using prompt
Input:
Write-Host "Demo of getting multiple inputs from user" -ForegroundColor Green
$ainp = @()
do {
$ips = Read-Host -Prompt "Enter ur name"
if ($ips -ne '') {$ainp += $ips}
}
until ($ips -eq 'end')
Write-Host "Entered names are" -ForegroundColor Green
$ainp
Write-Host "Demo of getting multiple user input without loop"
$dummy = "`n"
[string[]] $nl= @()
$nl = READ-HOST -Prompt "enter the names separated by comma"
$nl = $nl.Split(',').Split(' ')
Write-Host "Entered values are" -ForegroundColor Green
$dummy + $nl
Output:
Conclusion
Thus, the article covered in detail prompting for user input in PowerShell. Along with explaining the various approaches, it provides valuable examples. It showed multiple ways of getting input in a secured format and GUI. It also explained how to get multiple input values from users with and without a loop. To learn more details, writing and practicing sample scripts is advisable.
Recommended Articles
This is a guide to PowerShell prompt for input. Here we discuss Introduction, syntax, and parameters, examples with code implementation. You may also have a look at the following articles to learn more –