Implementing -whatIf in a PowerShell function

I've got a function in a module I use to deploy binaries and files to various websites and wanted to usethe -whatIf switch on the function and have all the new-Item and copy-Item commandlets pickup the switch.

It took a bit of working out from various sources how to do it. It's actually really simple. Before the param declaration in the function, simply add this cmdltbinding:


And the -whatIf (and -confirm) switch is passed through the pipeline to the commandlets.

So the function looks like this:

function copy-Files
    # Function here

That's it!