PowerShell Basics Level: Beginner Chapter 4: Everyday Commands That Save Hours
Verbose, WhatIf, and Confirm
Three safety switches change how a command behaves: Verbose explains what it is doing, WhatIf previews without changing anything, and Confirm asks before each change.
Lesson 2 of 4 in Chapter 4: Everyday Commands That Save Hours. Previous step in this path: Select-Object, Where-Object, and Sort-Object. 3 learning objectives, 6 examples, and a knowledge check.
Start: what you will learn
Three safety switches change how a command behaves: Verbose explains what it is doing, WhatIf previews without changing anything, and Confirm asks before each change.
Learning objectives
- Explain what -Verbose, -WhatIf, and -Confirm each do
- Preview a change safely before running it for real
- Choose the right safety switch for a risky command
Concept in simple words
What it is: -Verbose, -WhatIf, and -Confirm are safety switches you add to a command to change how it behaves. -Verbose asks the command to explain what it is doing while it works. -WhatIf asks it to describe what it would do, without doing it. -Confirm asks you to say yes before each change is made. They do not change what the command is for, they change how carefully it proceeds.
Why it works
Why you need it: many beginner mistakes are not typing mistakes, they are target mistakes. The command was correct, but it acted on the wrong file, the wrong service, or far more items than expected. A preview catches that mistake while it is still cheap to fix. A prompt catches it at the last moment before the change. A verbose explanation helps you understand a command that seems to do nothing, so you can tell the difference between "it worked quietly" and "it never ran".
Why it works this way: many commands that change something have a built in safety layer, called ShouldProcess. A switch is an extra word starting with a hyphen that changes a detail of a command, without changing the command itself. -WhatIf talks to that safety layer and asks it to describe the action instead of doing it. -Confirm talks to the same layer and asks it to pause for your approval. -Verbose is a common parameter, which means PowerShell adds it to many cmdlets, and it asks the cmdlet to send extra explanation messages while it works.
Which commands support these switches: -Verbose is widely available, but it only shows extra messages if the cmdlet actually produces them. If a cmdlet has nothing extra to say, -Verbose changes nothing on screen, and that is normal, not an error. -WhatIf and -Confirm only work on cmdlets that support the safety layer. Read only commands such as Get-Service do not change anything, so a preview switch on them has nothing useful to preview. If PowerShell tells you that a parameter cannot be found, that is a sign the cmdlet does not support that switch, so stop, check the help for that cmdlet, and do not assume you are protected.
Think of it like this
Think of it like this: -Verbose is like a friend narrating each step while they work, so you can follow along. -WhatIf is like asking what would happen if you pressed the button, while keeping your finger off it. -Confirm is like a friend pausing and asking, are you sure, before each step that cannot be undone easily. Narration, preview, and approval are three different kinds of safety, and you choose the one that fits the risk.
How it works, step by step
How it works, step by step: first, write the changing command exactly as you would run it for real. Next, add -WhatIf and run it, then read the preview slowly and check the target name and the action. If the preview is wrong, fix the command and preview again. Only when the preview looks right, remove -WhatIf, or add -Confirm if you want to approve each change one by one as it happens. After a real change, run a read only Get command again to confirm the new state, because a command that ran without an error is not the same as a change you have verified.
Reading a WhatIf preview: a preview line usually reads like "What if: Performing the operation" followed by the action and the target. Read the target first, because that is where most mistakes hide. Ask yourself three questions. Is this the right target, is this the right action, and is this the only target, or are there more items behind it? If the preview lists more items than you expected, your filter was too broad, so narrow it before you run anything for real.
Reading a Confirm prompt: when -Confirm pauses, PowerShell shows the action and the target, and offers choices such as Y for Yes, A for Yes to All, N for No, L for No to All, S for Suspend, and ? for Help. Yes approves only this one item. Yes to All approves this item and every remaining item without asking again, so only choose it when you have already checked the whole set. No skips this item and moves to the next. No to All stops approving the rest. Suspend pauses the prompt so you can look around, and Help explains the choices. Never press Yes to All out of impatience, because that throws away the very protection you asked for.
What -Verbose shows, and what it does not show: verbose messages describe steps, targets, and progress, and they help you follow a command that would otherwise be silent. Verbose messages are not the same as normal output, so you cannot pipe them into the next command as data, and you cannot treat a verbose line as proof that a change happened. Treat verbose output as narration, not as a result. If you need the result, look at the normal output, or run a Get command afterwards to check the state.
Preference variables, explained simply: PowerShell also has settings that control this behaviour for a whole session. $WhatIfPreference can turn previews on for commands that support them, and $ConfirmPreference controls how serious an action must be before PowerShell asks on its own. By default, $ConfirmPreference is High, which means only high impact actions prompt on their own, while medium impact actions do not. Adding -Confirm forces a prompt for that one command, and -Confirm:$false turns prompting off for that one command. You do not need to memorise these settings yet, but knowing they exist explains why two commands can behave differently even when neither one has -Confirm typed out.
Combining the switches: you can use -WhatIf and -Verbose together to get a narrated preview, which is a good way to study a new changing command. You can use -Confirm together with a narrow filter, so you approve a small, known set one by one. What you should not do is use -WhatIf and -Confirm together and expect a neat result, because a preview means nothing runs, so there is nothing left to confirm. Pick one job at a time: preview, or approve, or narrate.
Let us walk through an example: in Stop-Service -Name Spooler -WhatIf, Stop-Service is the command that would stop a service. -Name Spooler says which service is the target. -WhatIf turns the whole thing into a preview, so nothing is stopped. Look for a line that says what would happen and names Spooler, and treat that line as your check, not as proof that anything changed. Warning: the same command without -WhatIf would really stop the service, so only remove -WhatIf when you mean it, and only on a target you are sure about.
A safe working habit for changing commands: start with a read only Get command to see the current state and the exact names. Next, run the changing command with -WhatIf and read every preview line. Then, for a small set, run it with -Confirm and approve each item deliberately. Finally, run the Get command again to verify the new state. This habit takes a few extra seconds, and it saves you from the kind of mistake that takes far longer to undo, if it can be undone at all.
When to use it
When to use it, and when not to: use -WhatIf before any command that deletes, stops, or changes something you care about, and use -Confirm when each item deserves its own yes or no. Use -Verbose when a command seems to do nothing and you want to see its steps. Do not use -Confirm for hundreds of items, because you will be answering prompts all day, and do not trust a preview if nothing prints, because some commands do not support -WhatIf. Also, do not use -WhatIf as a substitute for a backup or for checking your filter, because a preview only shows what the command thinks it will do, and a wrong filter can still produce a confident looking preview.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
Command -Verbose | Command -WhatIf | Command -ConfirmExample 1
Previews stopping the service, without stopping it. Read the preview, then decide. Look for the word Spooler as the target, and check that no other service is named. Warning: removing -WhatIf would really stop the service.
Stop-Service -Name Spooler -WhatIfIllustrative output: What if: Performing the operation "Stop-Service" on target "Spooler".
Example 2
Asks before deleting, so a typo in the path does not cost you a file. -Path names the exact file, and -Confirm pauses for your answer. Warning: if you answer Yes, the file is deleted, so read the path in the prompt before you answer.
Remove-Item -Path C:\Temp\old.txt -ConfirmIllustrative output: Confirm: Are you sure you want to perform this action?
Example 3
Remove-Item is the command that deletes a file. -Path C:\Temp\old.txt names the exact file that would be deleted. -WhatIf turns the deletion into a preview only, so the file stays where it is while you check that the path is the one you meant.
Remove-Item -Path C:\Temp\old.txt -WhatIfIllustrative output: What if: Performing the operation "Remove-Item" on target "C:\Temp\old.txt".
Example 4
This is the read only check to run before and after a change. It changes nothing, so it is safe to run at any time. Before a change, it confirms the service exists and shows its current state. After a change, it confirms whether the state really changed. Look for the Status value in the output.
Get-Service -Name SpoolerIllustrative output: Name Status ---- ------ Spooler Running
Example 5
Stop-Service would stop the service, -Name Spooler names the target, and -Confirm pauses for approval before it acts. At the prompt, Y approves only Spooler, N skips it, and A would approve every remaining item without asking again, so use A only when you have checked the whole set. Warning: answering Yes really stops the service.
Stop-Service -Name Spooler -ConfirmIllustrative output: Confirm: Are you sure you want to perform this action?
Example 6
Combining -WhatIf with -Verbose gives a narrated preview. -WhatIf means nothing is deleted, and -Verbose asks for extra explanation while the preview is produced. Use this combination when you are learning a new changing command and want to see its reasoning. The file still stays untouched, because -WhatIf is present.
Remove-Item -Path C:\Temp\old.txt -WhatIf -VerboseIllustrative output: What if: Performing the operation "Remove-Item" on target "C:\Temp\old.txt".
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Trusting a -WhatIf preview for a command that does not support it. If no preview prints, the command may have run for real.
- Using -Confirm on a command that changes hundreds of items, then answering each prompt by hand. Filter first, or use -WhatIf to check the set.
- Running a command with -WhatIf, seeing the preview, and walking away thinking the change already happened. A preview changes nothing, so you still need to run the command for real, without -WhatIf, when you are ready.
- Pressing Yes to All at a Confirm prompt without reading the remaining targets. Yes to All removes the protection for every item that follows, so only use it when the whole set is already checked.
- Thinking -Verbose changes what a command does. -Verbose only adds explanation messages, it does not make a command safer, and it does not change its target or its action.
- Adding -WhatIf to a read only Get command and feeling protected, when there was nothing to protect. Safety switches matter most on commands that delete, stop, move, or change something.
- Running the real change straight after a preview, without rechecking the target list. If anything changed between the preview and the real run, the real run can act on a different set, so preview again if time has passed or the data may have changed.
Knowledge check
Knowledge check
Which switch previews a change without making it?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: preview first, change second. -WhatIf shows, -Confirm asks, and -Verbose explains. Build the habit now, and you will avoid most of the scary mistakes beginners make. The safest command is not the shortest one, it is the one whose target you have already checked twice.
Next step: Formatting, input, and output display