PowerShell Scripting Basics: -WhatIf and -Confirm with Examples

PowerShell Scripting Basics_ -WhatIf and -Confirm Paramaters

Introduction to PowerShell Scripting -WhatIf and -Confirm

PowerShell’s WhatIf and confirm are two great commands for testing complicated scripts without risking the code running amok.  For example, if you decide to delete files by using a script containing wildcards, there could be all manner of unexpected side effects.  By employing PowerShell, and appending the -WhatIf switch, you get a preview of would happen without risking any damage.

PowerShell WhatIf and -Confirm Commands

Mission to Delete Files

Let us take a real life example, we wish to delete files, but because we are using wildcards we are concerned about deleting the wrong type of file.  Instead of gung-ho Guy deleting the files – ready or not, we will take the cautious approach and append -WhatIf.  The result is PowerShell completes the command and shows us the result, but does not delete any files.  Incidentally, I cannot find a delete verb in PowerShell, there is however, a remove verb.

PowerShell -WhatIf Example

By adding -WhatIf at the end of the command we are saying to PowerShell: ‘Just test, don’t actually make any permanent changes’.  Please note, there could be serious consequences if you don’t use the -WhatIf switch.  If you don’t understand what you are doing, you could delete all your .txt files.

# PowerShell -WhatIf safety parameter
Get-Childitem C:\SomeFile\*.txt -Recurse | Remove-Item -WhatIf

Note 1: For safety I chose a fictitious folder, just in case the script ran amok.

A breakdown of what the above script achieves

Get-Childitem  (Rather like dir)
C:\SomeFile (Location to start)
-Include *.txt  (The pattern to look for)
-Recurse (Search subdirectories)
| Remove-Item (The equivalent of Delete)
-WhatIf (PowerShell please test, but don’t actually complete the operation, in this case, just show me which files with a .txt extension would be deleted if I removed the -WhatIf).

Another example of the  -WhatIf switch

Guy Recommends:  Network Performance Monitor (FREE TRIAL)Review of Orion NPM v11.5

SolarWinds Network Performance Monitor (NPM) will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM on a 30-day free trial.

SolarWinds Network Performance Monitor Download 30-day FREE Trial

PowerShell -Confirm Example

Here is another PowerShell parameter that you append to a ‘normal’ script – confirm.  It really is a case of confirm by name, and confirm by nature.  PowerShell says to you: ‘Do you really want to do this?’

# PowerShell -Confirm parameter
Get-Childitem C:\Dzxocs\*.* -Include *.txt -Recurse | Remove-Item -Confirm

Note 2: For safety I chose a fictitious folder, just in case the script went wrong.

The result of -Confirm is that PowerShell presents you with choices, however, remember this is now ‘live’ therefore if you press [Y] or [A] then files will be deleted.

[Y] Yes [A] Yes to all  [N] No  [L] No to all [S] Suspend

PowerShell -Confirm:$False

I stumbled upon the $False commands for unattended scripts.  What I am thinking is that if you are running scripts which require a response, then you could try appending -Confirm:False  (do remember that colon).

# PowerShell -Confirm for unattended machines
Restart-Service Bits -Confirm:$False

Get Help About Variable Preferences »

How To Research PowerShell Cmdlets Containing -Confirm and -WhatIf

My idea behind this research is to list the PowerShell cmdlets that contain ‘confirm’ in their parameters.

# Research PowerShell parameters for 'Confirm'
Get-Command | where { $_.parameters.keys -Contains "Confirm"}

Refine Get-Command with -CommandType

# PowerShell Confirm cmdlets
Get-Command -commandType cmdlet `
| where { $_.parameters.keys -Contains "Confirm"} | Format-Table Name


Note 3: You could substitute ‘WhatIf’ for ‘Confirm’.

Summary of PowerShell -WhatIf and -Confirm Commands

PowerShell’s WhatIf switch is hand if you need a dry run of your script.  Once you have used PowerShell’s -Confirm, or -WhatIf commands you will think, ‘Why don’t all scripting languages have these safety features’.

See more examples of PowerShell syntax

PowerShell Tutorials  • Syntax  • PowerShell functions  • Plist  • RegEx  • -Com

PowerShell -confirm  • WhatIf  • -Match  • -Like  • -Online  • Where  • Free WMI Monitor

-ErrorAction  • -Replace  • Windows PowerShell  • PowerShell module directory

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.