EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell prompt for input

PowerShell prompt for input

Updated June 28, 2023

PowerShell prompt for input

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.

ADVERTISEMENT
Popular Course in this category
WINDOWS POWERSHELL Course Bundle - 7 Courses in 1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

powershell promt input

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:

powershell promt input 2

powershell promt input 2-1

powershell promt input 2-2

powershell promt input 2-3

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:

Data entry form

example 3

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 4

example 4-1

example 4-2

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

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:

example 7

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 –

  1. PowerShell join string
  2. PowerShell Exit
  3. String in PowerShell
  4. PowerShell Wait
ADVERTISEMENT
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW