X

How to Prompt a User for Input using PowerShell

You can use the read-host command-let to get an input from a user during program execution.

PowerShell Input Example

Here is a related example, the program gets two numbers in string format and converts them into integers and then sums up those numbers and finally displays the result. The -prompt parameter is used to display a message or information to the user with colon appended at the end.

The script

$num1 = read-host -Prompt "Enter Ist number"
$num2 = Read-Host -Prompt "Enter Second number" 

[int]$Num1 = [convert]::ToInt32($num1, 10)
[int]$Num2 = [convert]::ToInt32($num2, 10)

$sum = $num1 + $num2

Write-Host "Result: $sum"
Karim Buzdar: About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various websites.
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings