Josh Petitt
Josh Petitt

Reputation: 9579

PowerShell how to add a custom common parameter?

I would like to make a custom -Force switch a common parameter.

I have three functions that each take the parameter.

function A calls function B, function B calls function C

I would like to call function A with the custom -Force parameter and have it propagate as a common parameter, without having to do something like this:

function B() {

    [cmdletbinding()]

    param([switch] $Force)   

    # NOTE: THIS CHECK IS THE PART I WANT TO ELIMINATE
    # I WANT -Force TO BE PASSED ALONG AS A COMMON PARAMETER
    if ($Force) {
        C -Force
    }
    else {
        C
    }

}

Upvotes: 4

Views: 2071

Answers (2)

FoxDeploy
FoxDeploy

Reputation: 13537

The common parameters are built into PowerShell itself. You may be interested to know that -Force is not a common param, rather it is a switch built into each Cmdlet. So, if you want to implement -Force into all of your Function/cmdlets, you're going the right route by defining a switch parameter for each cmdlet.

For your reference, here is a list of the common parameters.

-Debug (db)
-ErrorAction (ea)
-ErrorVariable (ev)
-OutVariable (ov)
-OutBuffer (ob)
-PipelineVariable (pv)
-Verbose (vb)
-WarningAction (wa)
-WarningVariable (wv

 risk mitigation parameters
-WhatIf (wi)
-Confirm (cf)

Upvotes: 1

briantist
briantist

Reputation: 47802

I don't think you can make it a common parameter, but you can do this:

Function B() {

    [cmdletbinding()]

    param([switch] $Force)   

    C -Force:$Force

    }

}

Upvotes: 5

Related Questions