Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Add WhatIf Support to Your PowerShell Scripts

Posted on December 2, 2011

In one of my recent articles for SMB IT, I included a PowerShell module. In the article I referenced that I included support for -Whatif in one of the functions. I was asked on Twitter to explain what I meant and how it works. So here goes.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

There are a few ways you can add support for -WhatIf in your PowerShell scripts and functions. The first thing you need to do is add the cmdletbinding attribute at the beginning of your script, before the Param() section, and set SupportsShouldProcess to $True.

[cc lang="PowerShell"]
[cmdletbinding(SupportsShouldProcess=$True)]
[/cc]

In a function you might do this:

[cc lang="PowerShell"]
Function Remove-Data {
<# comment based help goes here #>
[cmdletbinding(SupportsShouldProcess=$True)]
Param()
...
[/cc]

Even if you have no parameters, you should still add the Param() statement for this to work properly. Once this is in place, when you run the function it will automatically support -WhatIf. If your function is calling cmdlets that also support -WhatIf, such as Remove-Item, then they will "inherit" the parameter.

[cc lang="PowerShell"]
PS C:\> Remove-Data c:\work\f.txt -whatif
What if: Performing operation "Remove File" on Target "C:\work\f.txt".
[/cc]

In the function referenced in my SMBIT article that is exactly what I am doing. I'm merely passing -WhatIf on to other cmdlets. However, you can also write your own WhatIf handling. You still need the cmdletbinding attribute. But now, when you get to the part of your script where you want to add -WhatIf support you can add code like this:

[cc lang="PowerShell"]
if ($pscmdlet.ShouldProcess($Path)) {
Write-Host "Removing $path" -foregroundcolor Yellow
#my code to do something with $Path goes here
} #shouldprocess
[/cc]

Now when I run my function I get a slightly different output:

[cc lang="DOS"]
PS C:\> remove-data c:\work\f.txt -whatif
What if: Performing operation "Remove-Data" on Target "c:\work\f.txt".
[/cc]

The operation is now the name of my function. This is a useful technique if you want to include WhatIf support using cmdlets that don't support it. I use conditional checks like this to test the flow of my more complicated scripts without actually having them do anything.

As you can see, it is not that difficult and I encourage you to see how you can incorporate these feature into your PowerShell work.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Add WhatIf Support to Your PowerShell Scripts”

  1. JV says:
    December 2, 2011 at 12:37 pm

    Good article.

    This can aslo be helpful:
    $pscmdlet.ShouldProcess($path,”Delete”)

    There are two other variations of ‘ShouldProcess’

  2. Pingback: AD Clean up with Powershell for Beginners. « Tim Bolton – MCITP – MCTS

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d