Skip to content

Commit b68d9bd

Browse files
committed
Update -Force example
1 parent 89f5083 commit b68d9bd

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

reference/docs-conceptual/learn/deep-dives/everything-about-shouldprocess.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: ShouldProcess is an important feature that is often overlooked is. The WhatIf and Confirm parameters make it easy to add to your functions.
33
ms.custom: contributor-KevinMarquette
4-
ms.date: 11/16/2022
4+
ms.date: 09/24/2024
55
title: Everything you wanted to know about ShouldProcess
66
---
77
# Everything you wanted to know about ShouldProcess
@@ -11,18 +11,18 @@ One important feature that is often overlooked is `-WhatIf` and `-Confirm` suppo
1111
add to your functions. In this article, we dive deep into how to implement this feature.
1212

1313
> [!NOTE]
14-
> The [original version][original version] of this article appeared on the blog written by [@KevinMarquette][@KevinMarquette]. The
15-
> PowerShell team thanks Kevin for sharing this content with us. Please check out his blog at
16-
> [PowerShellExplained.com][PowerShellExplained.com].
14+
> The [original version][original version] of this article appeared on the blog written by
15+
> [@KevinMarquette][@KevinMarquette]. The PowerShell team thanks Kevin for sharing this content with
16+
> us. Please check out his blog at [PowerShellExplained.com][PowerShellExplained.com].
1717
1818
This is a simple feature you can enable in your functions to provide a safety net for the users that
1919
need it. There's nothing scarier than running a command that you know can be dangerous for the
2020
first time. The option to run it with `-WhatIf` can make a big difference.
2121

2222
## CommonParameters
2323

24-
Before we look at implementing these [common parameters][common parameters], I want to take a quick look at how
25-
they're used.
24+
Before we look at implementing these [common parameters][common parameters], I want to take a quick
25+
look at how they're used.
2626

2727
## Using -WhatIf
2828

@@ -516,6 +516,15 @@ function Test-ShouldProcess {
516516
[Switch]$Force
517517
)
518518
519+
if (-not $PSBoundParameters.ContainsKey('Confirm')) {
520+
Write-Verbose "Setting `$Confirm = $false"
521+
$Confirm = $false
522+
} else {
523+
Write-Verbose "Setting `$Confirm = $($PSBoundParameters.Confirm)"
524+
# Set $Confirm to the value passed by the user
525+
$Confirm = $PSBoundParameters.Confirm
526+
}
527+
519528
if ($Force -and -not $Confirm){
520529
$ConfirmPreference = 'None'
521530
}
@@ -526,17 +535,21 @@ function Test-ShouldProcess {
526535
}
527536
```
528537

529-
We add our own `-Force` switch as a parameter. The `-Confirm` parameter is automatically added
530-
when using `SupportsShouldProcess` in the `CmdletBinding`.
538+
We add our own `-Force` switch as a parameter. The `-Confirm` parameter is automatically added when
539+
using `SupportsShouldProcess` in the `CmdletBinding`. However, when you use `SupportsShouldProcess`,
540+
PowerShell doesn't add the `$Confirm` parameter to the function. If you are running in Strict Mode
541+
and try to use the `$Confirm` variable before it has been defined, you get an error. To avoid the
542+
error you can use `$PSBoundParameters` to test if the parameter was passed by the user.
531543

532544
```powershell
533-
[CmdletBinding(
534-
SupportsShouldProcess,
535-
ConfirmImpact = 'High'
536-
)]
537-
param(
538-
[Switch]$Force
539-
)
545+
if (-not $PSBoundParameters.ContainsKey('Confirm')) {
546+
Write-Verbose "Setting `$Confirm = $false"
547+
$Confirm = $false
548+
} else {
549+
Write-Verbose "Setting `$Confirm = $($PSBoundParameters.Confirm)"
550+
# Set $Confirm to the value passed by the user
551+
$Confirm = $PSBoundParameters.Confirm
552+
}
540553
```
541554

542555
Focusing in on the `-Force` logic here:
@@ -561,7 +574,7 @@ if ($PSCmdlet.ShouldProcess('TARGET')){
561574
If someone specifies both `-Force` and `-WhatIf`, then `-WhatIf` needs to take priority. This
562575
approach preserves `-WhatIf` processing because `ShouldProcess` always gets executed.
563576

564-
Do not add a check for the `$Force` value inside the `if` statement with the `ShouldProcess`. That is
577+
Don't add a test for the `$Force` value inside the `if` statement with the `ShouldProcess`. That is
565578
an anti-pattern for this specific scenario even though that's what I show you in the next section
566579
for `ShouldContinue`.
567580

0 commit comments

Comments
 (0)