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"

Leave a Comment