Scripting Games 2013: Use of SupportsShouldProcess in Functions/Scripts

Edit: I was caught speeding when I put up this post. Move-Item will pick up on SupportsShouldProcess and handle the –Whatif and –Confirm without having to supply the $Pscmdlet.SupportsShouldProcess().  In fact, adding this to a cmdlet that already supports it natively will throw up 2 prompts with –Confirm instead of 1. –WhatIf will only display once.

You should still be mindful of what you are doing with this in the case of cmdlets and other things you are doing which may not have native support for this. Rather than completely re-writing this post, it will stand to show that no one here is perfect and we are learn from one another as well as make mistakes Smile!

While reviewing and voting on the entries for Event 1, I have been seeing some entries where they have the following:


Ok, great! They are going to implement –WhatIf and –Confirm with their function so the user can take advantage of those parameters. But what I see later is rather disheartening.

Move-Item -Source $Source -Destination $Destination

Ok, this is just an example, but does represent exactly what I see and what you are seeing as well: Nothing has been implemented to support the SupportsShouldProcess attribute that was defined earlier!

Truth be told, it is incredibly simple to implement this into your code. Take a look below:

Function Move-LogFile {
    Param (
    If ($Pscmdlet.ShouldProcess($File,"Move Item")) {
        Move-Item -Path $File -Destination $Destination

Again, very basic function to prove the point of implementing –WhatIf. Just use an If statement with the $Pscmdlet.ShouldProcess() method and supply the ‘Target’ and an ‘Action’ parameter and you have just successfully set up the –WhatIf support.

 Move-LogFile -File "SomeLogFile.log" -Destination "C:\Backup" -WhatIf


Very simple to implement and it will save you from being docked points for only having this half done.

This entry was posted in Scripting Games 2013, Tips and tagged , , , . Bookmark the permalink.

5 Responses to Scripting Games 2013: Use of SupportsShouldProcess in Functions/Scripts

  1. Hey Boe! Nice Article and thanks for your useful articles!!

    My question is: Is there a way to find if a cmdlet support natively SupportsShouldProcess ?

    I submit my script for the Advanced track and just adding [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact=”Medium”)] allows to use confirm and whatif. Is there something I don’t get ?

    Thanks for your help

  2. SupportsShouldProcess will work fine with Move-Item and New-Item if the $ConfirmPreference is set to ‘Low’ or maybe ‘Medium’ (not sure what New/Move-Item cmdlets default to), but certainly works without the $PSCmdlet.ShouldProcess call if you just include the -Confirm parameter; I’m curious, do you get double prompted if you include the -Confirm parameter when calling your script, once by your script, and once by New/Move-Item?

    • Boe Prox says:

      You are absolutely correct! Move-Item does natively support the SupportsShouldProcess attribute and will in fact double prompt you for -Confirm if used with Move-Item. I will update this article to reflect that and use a better example for this.

      Thanks for the comment!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s