Passing Switches to Child Functions

powershell edit

Just a quick post to demonstrate how to pass switches to child functions in PowerShell.

If you’re using [CmdletBinding()] in your functions, you’ve enabled additional switches and parameters on your function with almost no effort. These switches include -verbose and -debug as well as a few others, and because of the way [CmdletBinding()] works, you don’t need to pass those switchs on down to child functions. You do however need to pass custom switches.

In the example below, I simply demonstrate how switchs work when passing them to sub functions.

As you can see, the $Custom switch is passed down by calling -Custom:$Custom, and the rest of the switches are magically passed for you. That’s all there is to it.


comments powered byDisqus