In this tutorial, I will explain how to use the Read-Host cmdlet in PowerShell to prompt users for yes/no input. This is particularly useful when you need to make your scripts interactive, asking users to confirm actions before proceeding.
PowerShell Read-Host Yes/No
The Read-Host cmdlet in PowerShell reads a line of input from the console. It is commonly used to prompt users for input, which can then be used to control the flow of a script. This cmdlet can also handle various types of input, including yes/no responses.
Let me show you how to prompt for yes/no input using the PowerShell Read-Host cmdlet.
Basic Yes/No Prompt Example
Let’s start with a basic example where we ask the user if they want to continue with an action. We will use the names “John” and “Jane” to make the example more relatable.
Here is the PowerShell script.
$continue = Read-Host -Prompt "Do you want to continue, John? (yes/no)"
if ($continue -eq 'yes') {
Write-Output "Continuing with the action..."
} elseif ($continue -eq 'no') {
Write-Output "Action canceled by John."
} else {
Write-Output "Invalid input. Please enter 'yes' or 'no'."
}In this script, we use Read-Host to prompt John for a yes/no response. Based on the input, the script either continues with the action or cancels it.
Here is the exact output in the screenshot below:

Check out How to Securely Handle Passwords with PowerShell Read-Host?
Loop Until Valid Input
To ensure that we get a valid response, we can loop until the user provides a correct input. This approach improves user experience by repeatedly prompting until a valid answer is given.
Let me show you an example and the complete PowerShell script.
do {
$response = Read-Host -Prompt "Do you want to proceed, Jane? (yes/no)"
switch ($response) {
'yes' { Write-Output "Proceeding with the operation..."; $valid = $true }
'no' { Write-Output "Operation aborted by Jane."; $valid = $true }
default { Write-Output "Invalid input. Please enter 'yes' or 'no'."; $valid = $false }
}
} until ($valid)In this example, we use a do-while loop combined with a switch statement to handle the yes/no input. The loop continues until Jane provides a valid response.
Here is the exact output in the screenshot below:

Read How to Use PowerShell Read-Host to Enter Multiple Lines?
Using Read-Host in a Function
Encapsulating the prompt logic within a function can be beneficial for more complex scripts. This makes the script modular and easier to maintain.
Here is the complete script to write the logic inside a PowerShell function.
function Confirm-Action {
param (
[string]$PromptMessage = "Do you want to proceed? (yes/no)"
)
do {
$response = Read-Host -Prompt $PromptMessage
switch ($response) {
'yes' { return $true }
'no' { return $false }
default { Write-Output "Invalid input. Please enter 'yes' or 'no'." }
}
} until ($response -eq 'yes' -or $response -eq 'no')
}
if (Confirm-Action -PromptMessage "Shall we delete the records, Alex? (yes/no)") {
Write-Output "Records deleted."
} else {
Write-Output "Deletion aborted by Alex."
}In this function, Confirm-Action prompts the user with a customizable message. The function returns a boolean value based on the user’s input, which can be used to control the flow of the script.
Conclusion
In this tutorial, I explained how to use Read-Host to prompt for yes/no input in PowerShell. I also showed you how to implement these techniques with examples using functions and loops.
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.