Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you programmatically get a list of all common parameters?

Powershell Cmdlets inherit a bunch of common parameters. Some cmdlets I write end up with predicates that depend on which parameters are actually bound. This often leads to filtering out common parameters which means you need a list of common parameter names.

I also expect there to be difference in the list of common parameters from one version of powershell to another.

All of this boils down to this question:

How do you programmatically determine the list of common parameters?

like image 546
alx9r Avatar asked Apr 29 '15 23:04

alx9r


People also ask

What are the common parameters in PowerShell?

Some of the PowerShell common parameters are Verbose, Debug, ErrorAction, ErrorVariable, OutVariable, and OutBuffer. For a full list of the common parameters and more details on their usage, run Get-Help about_CommonParameters . PowerShell offers two risk mitigation parameters: WhatIf and Confirm.

How do I show all parameters in PowerShell?

We can use the command GET-Command to display all the parameters. We can also use the command Get-Help to display all the available parameters with details.

What is OutVariable in PowerShell?

-OutVariableTo add the output to the variable, instead of replacing any output that might already be stored there, type a plus sign ( + ) before the variable name. For example, the following command creates the $out variable and stores the process object in it: PowerShell Copy.

What is Pipelinevariable in PowerShell?

PowerShell has a unique variable called the pipeline variable ($_ or $PSItem). Due to PowerShell's ability to pipe entire objects from one command to another, it needed a way to represent that object that was traversing the pipeline.


1 Answers

What about these static properties?

[System.Management.Automation.PSCmdlet]::CommonParameters
[System.Management.Automation.PSCmdlet]::OptionalCommonParameters

The existing common parameters is the combination of both lists:

CommonParameters: Lists the common parameters that are added by the PowerShell engine to any cmdlet that derives from PSCmdlet.

OptionalCommonParameters: Lists the common parameters that are added by the PowerShell engine when a cmdlet defines additional capabilities (SupportsShouldProcess, SupportsTransactions)

i.e. All of them can exist, but the optional ones only exists if the cmdlet supports them. For detailed info see Cmdlet Class

like image 59
Rohn Edwards Avatar answered Sep 28 '22 04:09

Rohn Edwards