Search Wiki:

UseSwitchParameterInsteadOfBoolean


TypeName UseSwitchParameterInsteadOfBoolean
CheckId PS1014
Error-Level Warning
Category Microsoft.PowerShell
Certainty 95%
BreakingChange Breaking
Cause A cmdlet parameter has the Boolean type.
Rule description Cmdlets should use the SwitchParameter parameter type instead of the Boolean type. Switch parameters provide a better user experience because they do not require a parameter value.
How to fix violations Change the type of the parameter to SwitchParameter. If a cmdlet parameter accepts three valid values, $true, $false and “other”, you can fix the rule violation by changing the parameter type to Nullable<bool>. Typically, parameters require a third "other" value when the cmdlet can change a Boolean property of an object. The "other" value means "do not change the current value of the property."
When to exclude warnings Do not suppress warnings from this rule.
Related rules UseRecommendedParameterTypes
See also FxCop Rules for Windows PowerShell

Examples


    [Cmdlet("Write", "String")]
    public class WriteStringCommand : PSCmdlet
    {
        // Rule violation – bool parameter type.
        // (To fix, use SwitchParameter type.)
        [Parameter]
        public bool PassThru
        {
            get { return this.passthru; }
            set { this.passthru = value; }
        }
        private int passthru;
 
        // ...
    }

Back to FxCop Rules for Windows PowerShell.
Last edited Mar 26 2010 at 5:42 PM  by JuneB, version 1
Updating...
Page view tracker