As a PowerShell professional, I’ve often needed to prompt users for input during script execution. In this tutorial, I will explain the process of prompting for Yes/No input in PowerShell with examples.
PowerShell Prompt for Input Yes/No
PowerShell provides several ways to prompt users for input. The most common method is using the Read-Host cmdlet, but for Yes/No prompts, we often need more control.
Let me show you a few methods to prompt for input Yes/No in PowerShell.
Method 1: Using Read-Host for Simple Yes/No Prompts
The Read-Host cmdlet is easy to use for simple input prompts. Here’s how you can use it to ask a Yes/No question using the PowerShell script below.
$answer = Read-Host "Do you want to continue? (Yes/No)"
if ($answer -eq 'Yes') {
Write-Output "Continuing the process..."
} elseif ($answer -eq 'No') {
Write-Output "Process terminated."
} else {
Write-Output "Invalid input. Please enter Yes or No."
}In this example, the script prompts the user with “Do you want to continue? (Yes/No)” and captures the input. Based on the user’s response, it either continues the process or terminates it.
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Check out Add Comments in PowerShell
Method 2: Using a Custom Function for Yes/No Prompts
You can create a custom function for more robust input handling. This method is useful when you need to reuse the prompt multiple times in your script.
function Read-YesNoChoice {
param (
[string]$Title = "Please Choose",
[string]$Message = "Yes or No?"
)
$caption = "$Title"
$choices = "&Yes", "&No"
$defaultChoice = 1
$choice = [System.Windows.Forms.MessageBox]::Show($Message, $caption, [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question, [System.Windows.Forms.MessageBoxDefaultButton]::Button1)
return $choice
}
$choice = Read-YesNoChoice -Title "Confirmation" -Message "Do you want to proceed with the update?"
if ($choice -eq [System.Windows.Forms.DialogResult]::Yes) {
Write-Output "Proceeding with the update..."
} else {
Write-Output "Update canceled."
}This function, Read-YesNoChoice, uses a message box to prompt the user. It’s more interactive and visually appealing, making it suitable for scripts that require user confirmation.
Check out Restart a Computer Using PowerShell
Method 3: Using Choice Command for Yes/No Prompts
The Choice command is another way to prompt for Yes/No input in PowerShell, especially useful in scenarios where you need to ensure valid input.
Add-Type -AssemblyName Microsoft.VisualBasic
$caption = "Confirmation"
$message = "Do you want to delete the user account?"
$buttons = [Microsoft.VisualBasic.MsgBoxStyle]::YesNo + [Microsoft.VisualBasic.MsgBoxStyle]::Question
$result = [Microsoft.VisualBasic.Interaction]::MsgBox($message, $buttons, $caption)
if ($result -eq [Microsoft.VisualBasic.MsgBoxResult]::Yes) {
Write-Output "User account deleted."
} else {
Write-Output "Operation canceled."
}This method leverages the Microsoft.VisualBasic assembly to display a message box with Yes/No buttons, providing a clear and user-friendly way to capture input.
Check out How to Prompt for Input in PowerShell?
PowerShell Prompt for Input Yes/No – Examples
Now, let me show you a few practical examples where prompting for Yes/No input can be beneficial.
Example 1: Confirm User Deletion
Here is an example for confirming a user deletion.
$userName = "JohnDoe"
$confirmation = Read-YesNoChoice -Title "Delete User" -Message "Are you sure you want to delete the account for $userName?"
if ($confirmation -eq [System.Windows.Forms.DialogResult]::Yes) {
Write-Output "Deleting user account for $userName..."
# Code to delete the user account
} else {
Write-Output "Deletion canceled."
}Check out Get Computer Name in PowerShell
Example 2: Confirm Software Installation
Here is another example of confirmed software installation.
$softwareName = "Accounting Software"
$confirmation = Read-YesNoChoice -Title "Install Software" -Message "Do you want to install $softwareName?"
if ($confirmation -eq [System.Windows.Forms.DialogResult]::Yes) {
Write-Output "Installing $softwareName..."
# Code to install the software
} else {
Write-Output "Installation canceled."
}Example 3: Confirm Data Backup
Here is another example of
$backupPath = "C:\Backup"
$confirmation = Read-YesNoChoice -Title "Backup Data" -Message "Do you want to backup data to $backupPath?"
if ($confirmation -eq [System.Windows.Forms.DialogResult]::Yes) {
Write-Output "Backing up data to $backupPath..."
# Code to backup data
} else {
Write-Output "Backup canceled."
}Conclusion
In this tutorial, I used different methods to prompt for Yes/No input in PowerShell. For each method, I have provided examples.
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.