Confirmation Prompt in PowerShell
-
Use the
-Confirm
Switch for Confirmation Prompt in PowerShell -
Use the
if
Statement for Confirmation Prompt in PowerShell -
Use the
PromptForChoice()
Method for Confirmation Prompt in PowerShell

The general idea behind the confirmation prompt is to create an option to switch between different actions. For example, when the user chooses Yes
, the script should continue, and when the user selects No
, the script should be exited.
PowerShell can automatically ask for confirmation from the user before performing any action using the -Confirm
switch.
This tutorial will introduce different methods to ask for confirmation from users for any action to proceed in PowerShell.
Use the -Confirm
Switch for Confirmation Prompt in PowerShell
PowerShell has a set of preference variables that affect its operating environment and allows you to customize its behavior. $ConfirmPreference
is one of the preference variables determining whether PowerShell automatically prompts you for confirmation before running a cmdlet or function.
It helps to prompt confirmation before running cmdlets or functions with a low, medium, or high risk. Since most cmdlets and functions are of medium risk, it does not ask for confirmation if the variable’s value is set to High
.
$ConfirmPreference
Output:
High
You have to use the -Confirm
switch to the command to prompt the user for confirmation. It forces the command to display the confirmation prompt in PowerShell.
A simpler way to use the -Confirm
switch would be by using it in the Remove-Item
cmdlet, which can produce a confirmation action in simpler command.
The following command shows an example of its usage.
Remove-Item test.txt -Confirm
Output:
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Users\rhntm\test.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): N
In the GUI of PowerShell, it will look like this.
Use the if
Statement for Confirmation Prompt in PowerShell
You can also use the if
statement and provide the conditions to continue for users.
For example, the following command asks the user for confirmation if they want to continue. If the answer is equal to y
, only the command inside the braces is executed.
$confirmation = Read-Host "Do you want to continue?"
if ($confirmation -eq 'y') {
Write-Host "Welcome to DelftStack."
}
Output:
Do you want to continue?: y
Welcome to DelftStack.
The Read-Host
cmdlet is used to read the input from a user.
Use the PromptForChoice()
Method for Confirmation Prompt in PowerShell
In PowerShell, you can also prompt the users for confirmation using the PromptForChoice()
method. It displays a dialog that lets the user select one option from a set of options and proceed with the action according to their choices.
The following command is an example that prompts for a Yes/No
input to a user. If the user chooses Yes
, the first command inside the brackets {}
will be executed.
$title = 'Confirm'
$question = 'Do you want to continue?'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host 'Your choice is Yes.'
} else {
Write-Host 'Your choice is No.'
}
Output:
Confirm
Do you want to continue?
[Y] Yes [N] No [?] Help (default is "N"): Yes
Your choice is Yes.
As a result, the output Your choice is Yes.
gets printed.
If the user selects No
, the second command inside the brackets will be executed.
Output:
Confirm
Do you want to continue?
[Y] Yes [N] No [?] Help (default is "N"): No
Your choice is No.
Hence, Your choice is No.
is printed.
If you run the above script in the GUI version of PowerShell, you can view the confirm dialog box.
We hope this tutorial helps you understand how to prompt users for a Yes/No
confirmation in PowerShell.