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
@@ -1,6 +1,6 @@
---
description: Avoid Default Value For Mandatory Parameter
ms.date: 06/28/2023
ms.date: 05/28/2026
ms.topic: reference
title: AvoidDefaultValueForMandatoryParameter
---
Expand All @@ -10,13 +10,13 @@ title: AvoidDefaultValueForMandatoryParameter

## Description

Mandatory parameters should not have a default values because there is no scenario where the default
can be used. PowerShell prompts for a value if the parameter value is not specified when calling the
Mandatory parameters shouldn't have default values because there isn't a scenario where the default
can be used. PowerShell prompts for a value if the parameter value isn't specified when calling the
function.

## Example

### Wrong
### Noncompliant

```powershell
function Test
Expand All @@ -31,7 +31,7 @@ function Test
}
```

### Correct
### Preferred

```powershell
function Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: Switch Parameters Should Not Default To True
ms.date: 12/05/2024
ms.date: 05/28/2026
ms.topic: reference
title: AvoidDefaultValueSwitchParameter
---
Expand All @@ -15,18 +15,23 @@ treats a switch parameter as `true` when it's used with a command. If the parame
with the command, PowerShell considers the parameter to be false. Don't define `[Boolean]`
parameters.

You shouldn't define a switch parameter with a default value of `$true` because this isn't the
expected behavior of a switch parameter.
Don't define a switch parameter with a default value of `$true` because a switch parameter is
already `$false` when it isn't specified. Leave the switch parameter without a default value so it
behaves as designed.

## How

Change the default value of the switch parameter to be `$false` or don't provide a default value.
Write the logic of the script to assume that the switch parameter default value is `$false` or not
provided.
To fix this issue, don't assign a default value of `$true` to a `[switch]` parameter. Declare the
switch without a default value and write your logic so the parameter is treated as `$false` when
the caller doesn't supply it.

## More information

See [Strongly Encouraged Development Guidelines][01].

## Example

### Wrong
### Noncompliant

```powershell
function Test-Script
Expand All @@ -44,7 +49,7 @@ function Test-Script
}
```

### Correct
### Preferred

```powershell
function Test-Script
Expand All @@ -58,20 +63,10 @@ function Test-Script
[switch]
$Switch
)

begin {
# Ensure that the $Switch is set to false if not provided
if (-not $PSBoundParameters.ContainsKey('Switch')) {
$Switch = $false
}
}
...
}
```

## More information

- [Strongly Encouraged Development Guidelines][01]

<!-- link references -->

[01]: /powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines#parameters-that-take-true-and-false
Loading