Using WhatIf Parameter in PowerShell

The -WhatIf parameter in PowerShell scripts is typically used to avoid accidental changes to managed objects. Adding the -WhatIf parameter to the PowerShell command will display the objects to be changed by this command and the changes made. At the same time, no changes are actually made.

For example, you decided to delete some user accounts in the Active Directory domain according to certain criteria using the PowerShell script. To make sure that the PowerShell script deletes only the users you expect, you can add the –WhatIf parameter to the command.

With WhatIf, you can make sure that the changes made to these objects meet your expectations without worrying about changing these objects.
The -WhatIf parameter is a switch parameter and is found in many PowerShell cmdlets.

You can list the PowerShell cmdlets that support the –WhatIf parameter with the command:

Get-Command | where { $_.parameters.keys -Contains "WhatIf"}

powershell whatif

The -WhatIf parameter is added at the end of the PowerShell command. The presence of this cmdlet indicates that this command is executed in a test mode, and no changes will be made, but the list of objects will only be displayed on the PoSh console screen.

For example, the following command will select all inactive computers from the beginning of the year in the Active Directory domain and delete them. If, before deleting objects in AD, you want to see the list of computers to be deleted, add the –WhatIf switch to the command.

Search-ADAccount -AccountInactive -ComputersOnly -DateTime ‘1/1/2019’|Remove-ADComputer -WhatIf

The previous command will print the following text to the console:

What if: Performing the operation “Remove” on target “CN=us-PC1,OU=Workstations,OU=NY,DC=theitbros,DC=com”.

powershell whatif parameter

Together with the –WhatIf parameter, the –Confirm parameter is often remembered, which will ask you to confirm that you really want to perform this operation before performing an action. When using this switch, the command asks for confirmation for each action before continuing execution (by default, the mode –Confirm:$true is present in all PowerShell cmdlets starting with Clear-, Disable-, Dismount-, Move-, Remove-, Stop-, Suspend-, Uninstall-.

The code of the above PowerShell script with the –Confirm switch might look like this:

Search-ADAccount -AccountInactive -ComputersOnly -DateTime ‘1/1/2019’|Remove-ADComputer -confirm

use powershell whatif parameter

As you can see, a confirmation request has appeared:

Are you sure you want to perform this action?


Performing the operation “Remove” on target …

Usually, the –WhatIf parameter is used when testing and configuring PowerShell scripts, and in ready-made PoSh scripts, if you want to receive confirmation of a change, the –Confirm parameter is used.

I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.