WhatIf Parameter in PowerShelll

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.

You may also like:

How to transfer FSMO Roles From a Failed Domain Co... In case domain controller, which owns FSMO (Flexible Single Master Operation) roles, is fail (virus attack, fatal software problems or catastrophic ha...
Time Configuration for a Virtualized Domain Contro... Today we will talk about some of the features of time configuration on a virtualized domain controllers. Typically, the time synchronization scheme in...
Removing Old and Unused Drivers from Driver Store ... Each time you install or update your device driver, Windows OS (since Vista) continues to store the old version of the driver in the system Driver Sto...
Get-service: Checking the Status of Windows Servic... Using the Get-Service cmdlet you can get a list of all the services installed on the operating system, its status and startup type. This one and other...
How to Create a GUI for PowerShell Scripts? One of the significant drawbacks of PowerShell scripts, when used by users (not sysadmins or programmers), is its command-line interface. The result o...

Add Your Comment