Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ For more information about how background jobs are handled at the command line,

To write a cmdlet that can be run as a background job, you must complete the following tasks:

- Define an `asJob` switch parameter so that the user can decide whether to run the cmdlet as a background job.
- Define an `AsJob` `[switch]` parameter so that the user can decide whether to run the cmdlet as a
background job.

- Create an object that derives from the [System.Management.Automation.Job](/dotnet/api/System.Management.Automation.Job) class. This object can be a custom job object or a job object provided by Windows PowerShell, such as a [System.Management.Automation.PSEventJob](/dotnet/api/System.Management.Automation.PSEventJob) object.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ title: Cmdlet Dynamic Parameters
Cmdlets can define parameters that are available to the user under special conditions, such as when
the argument of another parameter is a specific value. These parameters are added at runtime and are
referred to as dynamic parameters because they're only added when needed. For example, you can
design a cmdlet that adds several parameters only when a specific switch parameter is specified.
design a cmdlet that adds several parameters only when a specific `[switch]` parameter is specified.

> [!NOTE]
> Providers and PowerShell functions can also define dynamic parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ about this attribute, see [Cmdlet Attribute Declaration](cmdlet-attribute-declar
### Cmdlet parameter

The public properties that define the parameters that are available to the user or to the
application that is running the cmdlet. Cmdlets can have required, named, positional, and *switch*
parameters. Switch parameters allow you to define parameters that are evaluated only if the
application that is running the cmdlet. Cmdlets can have required, named, positional, and `[switch]`
parameters. `[switch]` parameters allow you to define parameters that are evaluated only if the
parameters are specified in the call. For more information about the different types of parameters,
see [Cmdlet Parameters](cmdlet-parameters.md).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ public ScriptBlock Script
ScriptBlock script;
```

The `SimpleMatch` parameter is a switch parameter that indicates whether the cmdlet is to explicitly
match the patterns as they are supplied. When the user specifies the parameter at the command line
(`true`), the cmdlet uses the patterns as they are supplied. If the parameter is not specified
(`false`), the cmdlet uses regular expressions. The default for this parameter is `false`.
The `SimpleMatch` parameter is a `[switch]` parameter that indicates whether the cmdlet is to
explicitly match the patterns as they are supplied. When the user specifies the parameter at the
command line (`true`), the cmdlet uses the patterns as they are supplied. If the parameter is not
specified (`false`), the cmdlet uses regular expressions. The default for this parameter is
`false`.

```csharp
[Parameter]
Expand All @@ -160,12 +161,12 @@ public SwitchParameter SimpleMatch
private bool simpleMatch;
```

The `CaseSensitive` parameter is a switch parameter that indicates whether a case-sensitive search
is performed. When the user specifies the parameter at the command line (`true`), the cmdlet checks
for the uppercase and lowercase of characters when comparing patterns. If the parameter is not
specified (`false`), the cmdlet does not distinguish between uppercase and lowercase. For example
"MyFile" and "myfile" would both be returned as positive hits. The default for this parameter is
`false`.
The `CaseSensitive` parameter is a `[switch]` parameter that indicates whether a case-sensitive
search is performed. When the user specifies the parameter at the command line (`true`), the cmdlet
checks for the uppercase and lowercase of characters when comparing patterns. If the parameter is
not specified (`false`), the cmdlet does not distinguish between uppercase and lowercase. For
example "MyFile" and "myfile" would both be returned as positive hits. The default for this
parameter is `false`.

```csharp
[Parameter]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ title: How to Declare Cmdlet Parameters
---
# How to Declare Cmdlet Parameters

These examples show how to declare named, positional, required, optional, and switch parameters. These examples also show how to define a parameter alias.
These examples show how to declare named, positional, required, optional, and `[switch]`
parameters. These examples also show how to define a parameter alias.

## How to Declare a Named Parameter

Expand Down Expand Up @@ -69,7 +70,7 @@ For more information about the Parameter attribute, see [Parameter Attribute Dec
private string userName;
```

## How to Declare a Switch Parameter
## How to Declare a `[switch]` parameter

- Define a public property as type [System.Management.Automation.SwitchParameter](/dotnet/api/System.Management.Automation.SwitchParameter), and then declare the Parameter attribute.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: How to Declare Dynamic Parameters

This example shows how to define dynamic parameters that are added to the cmdlet at runtime. In this
example, the `Department` parameter is added to the cmdlet whenever the user specifies the
`Employee` switch parameter. For more information about dynamic parameters, see
`Employee` `[switch]` parameter. For more information about dynamic parameters, see
[Cmdlet Dynamic Parameters][02].

## To define dynamic parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ information about background jobs, see [Background Jobs](./background-jobs.md).

## To support jobs

1. Define an `AsJob` switch parameter so that the user can decide whether to run the cmdlet as a
job.
1. Define an `AsJob` `[switch]` parameter so that the user can decide whether to run the cmdlet as
a job.

The following example shows an AsJob parameter declaration.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ additional confirmation. For these cases, supplement the
to more finely control the scope of the **Yes to all** response to the confirmation prompt.

If a cmdlet calls the [System.Management.Automation.Cmdlet.ShouldContinue][02] method, the cmdlet
must also provide a `Force` switch parameter. If the user specifies `Force` when the user invokes
the cmdlet, the cmdlet should still call [System.Management.Automation.Cmdlet.ShouldProcess][03],
but it should bypass the call to [System.Management.Automation.Cmdlet.ShouldContinue][02].
must also provide a `Force` `[switch]` parameter. If the user specifies `Force` when the user
invokes the cmdlet, the cmdlet should still call
[System.Management.Automation.Cmdlet.ShouldProcess][03], but it should bypass the call to
[System.Management.Automation.Cmdlet.ShouldContinue][02].

[System.Management.Automation.Cmdlet.ShouldContinue][02] will throw an exception when it's called
from a non-interactive environment where the user can't be prompted. Adding a `Force` parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ cmdlet, do not make the `Process` parameter for another cmdlet a

If your parameter takes only `true` and `false`, define the parameter as type
[System.Management.Automation.SwitchParameter](/dotnet/api/System.Management.Automation.SwitchParameter).
A switch parameter is treated as `true` when it is specified in a command. If the parameter is not
included in a command, Windows PowerShell considers the value of the parameter to be `false`. Do not
define Boolean parameters.
A `[switch]` parameter is treated as `true` when it is specified in a command. If the parameter is
not included in a command, Windows PowerShell considers the value of the parameter to be `false`.
Do not define Boolean parameters.

If your parameter needs to differentiate between 3 values: $true, $false and "unspecified", then
define a parameter of type Nullable\<bool>. The need for a 3rd, "unspecified" value typically occurs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ method. We recommend that you call this method instead of the
[System.Management.Automation.Host.PSHostUserInterface.WriteLine](/dotnet/api/System.Management.Automation.Host.PSHostUserInterface.WriteLine)
methods.

You can provide a **PassThru** switch parameter for cmdlets that do not typically return objects.
When the **PassThru** switch parameter is specified at the command line, the cmdlet is asked to
return an object. For an example of a cmdlet that has a **PassThru** parameter, see
You can provide a **PassThru** `[switch]` parameter for cmdlets that do not typically return
objects. When the **PassThru** `[switch]` parameter is specified at the command line, the cmdlet is
asked to return an object. For an example of a cmdlet that has a **PassThru** parameter, see
[Add-History](/powershell/module/Microsoft.PowerShell.Core/Add-History).

### Error output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: Types of Cmdlet Parameters
# Types of Cmdlet Parameters

This topic describes the different types of parameters that you can declare in cmdlets. Cmdlet
parameters can be positional, named, required, optional, or switch parameters.
parameters can be positional, named, required, optional, or `[switch]` parameters.

## Positional and Named Parameters

Expand Down Expand Up @@ -98,18 +98,17 @@ public string UserName
private string userName;
```

## Switch Parameters
## `[switch]` parameters

PowerShell provides a [System.Management.Automation.SwitchParameter][02] type that allows you to
define a parameter whose default value `false` unless the parameter is specified when the cmdlet is
called. Whenever possible, use switch parameters instead of Boolean parameters.
called. Whenever possible, use `[switch]` parameters instead of Boolean parameters.

Consider the following example. Many PowerShell cmdlets return output. However, these cmdlets have a
`PassThru` switch parameter that overrides the default behavior. When you use the `PassThru`
`PassThru` `[switch]` parameter that overrides the default behavior. When you use the `PassThru`
parameter, the cmdlet returns output objects to the pipeline.

To define a switch parameter, declare the property as the `[SwitchParameter]` type, as shown in the
following sample.
The following sample shows how to define a `[switch]` parameter:

```csharp
[Parameter()]
Expand All @@ -135,18 +134,18 @@ protected override void ProcessRecord()
} // End ProcessRecord
```

By default, switch parameters are excluded from positional parameters. You _can_ override that in
the **Parameter** attribute, but it can confuse users.
By default, `[switch]` parameters are excluded from positional parameters. You _can_ override that
in the **Parameter** attribute, but it can confuse users.

Design switch parameters so that using parameter changes the default behavior of the command to a
less common or more complicated mode. The simplest behavior of a command should be the default
behavior that doesn't require the use of switch parameters. Base the behavior controlled by the
switch on the value of the switch, not the presence of the parameter.
Design `[switch]` parameters so that using the parameter changes the default behavior of the
command to a less common or more complicated mode. The simplest behavior of a command should be the
default behavior that doesn't require the use of `[switch]` parameters. Base the behavior
controlled by the `[switch]` parameter on the _value_ of the parameter, not its _presence_.

There are several ways to test for the presence of a switch parameters:
There are several ways to test for the presence of a `[switch]` parameter:

- `Invocation.BoundParameters` contains the switch parameter name as a key
- `PSCmdlet.ParameterSetName` when the switch defines a unique parameter set
- `MyInvocation.BoundParameters` contains the `[switch]` parameter name as a key
- `PSCmdlet.ParameterSetName` when the `[switch]` parameter defines a unique parameter set

For example, it's possible to provide an explicit value for the switch using `-MySwitch:$false` or
splatting. If you only test for the presence of the parameter, the command behaves as if the switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ If a provider doesn't implement any dynamic parameters, the provider help topic

1. Add the `PossibleValues` element and its child elements. Together, these elements describe the
values of the dynamic parameter. This element is designed for enumerated values. If the dynamic
parameter doesn't take a value, such as is the case with a switch parameter, or the values can't
be enumerated, add an empty `PossibleValues` element.
parameter doesn't take a value, such as is the case with a `[switch]` parameter, or the values
can't be enumerated, add an empty `PossibleValues` element.

The following table lists and describes the `PossibleValues` element and its child elements.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ aren't required, because they're one of several values that might be used with t
List all values of enumerations, such as the `-Type` parameter in the previous example, which can
be set to **basic** or **advanced**.

Switch parameters, such as `-List` in the previous example, don't have values.
`[switch]` parameters, such as `-List` in the previous example, don't have values.

1. Add angle brackets to parameters values that are placeholder, as compared to parameter values
that are literals.
Expand Down
2 changes: 1 addition & 1 deletion reference/docs-conceptual/lang-spec/chapter-02.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ matching parameters to arguments is called _parameter binding_.

There are three kinds of argument:

- Switch parameter ([§8.10.5][§8.10.5]) -- This has the form _command-parameter_ where
- `[switch]` parameter ([§8.10.5][§8.10.5]) -- This has the form _command-parameter_ where
_first-parameter-char_ and _parameter-chars_ together make up the switch name, which corresponds
to the name of a parameter (without its leading `-`) in the command being invoked. If the trailing
colon is omitted, the presence of this argument indicates that the corresponding parameter be set
Expand Down
Loading