Example of -WhatIf and -Confirm in PowerShell
# Usage examples
# .\whatif.ps1 "Hello, World!"
# .\whatif.ps1 "Hello, World!" -WhatIf
# .\whatif.ps1 "Hello, World!" -Confirm
# .\whatif.ps1 "Hello, World!" -Confirm:$False
# This is an example on how to use SupportsShouldProcess to implement
# the -WhatIf and -Confirm flags for your script
# Try setting the ConfirmImpact to "High" to enforce a confirmation from the
# user. This confirmation can be supressed by using the -Confirm:$False flag
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "Medium"
)]
param (
[string] $Greetings = "Hello, User"
)
$ErrorActionPreference = "Stop"
Write-Verbose ("Using greeting " -f $Greetings)
if($PSCmdlet.ShouldProcess($Greetings, "Greet user")){
Write-Verbose "greeting the user..."
Write-Output $Greetings
}