Using –SupportsShouldProcess in Powershell V2 ScriptCmdlets

Okay so I’m a bit of an early adopter. My main admin rig at work has been running V2 for the past year. In the last six months I’ve fallen in love with scriptcmdlets. One thing that constantly reminded me that this was still a CTP, and not a shipped product was –SupportsShouldProcess. The included about docs are misleading at best. The hidden user guide in %windir%System32WindowsPowerShellv1.0en-US had nothing, and perhaps the biggest shock of all. There was nothing on the web. The only thing I could find was this PDF from a Server 2k8 launch event. I started to think I was the only one who wanted this functionality. Well all I needed was handed to me on a golden platter this morning when Mr. Snover posted this. At last, a working example!

To keep this as easy as possible take the following V1 function:

112508-0406-usingsuppor1

Now let make it a V2 scriptCmdlet:

112508-0406-usingsuppor2

Finally let’s give this little guy some kick!

112508-0406-usingsuppor3

Why should you care? Well simple I added three lines of code and one parameter. Look at all the functionality the PowerShell Team provided me with that small amount of code!

112508-0406-usingsuppor4

There you have it script cmdlets ShouldProcess support. Hope that helps anyone who’s been trying to figure that out.

~Glenn

Leave a Reply