🎓 A1Learner
🔍
0 of 47 lessons completed

PowerShell Common Parameters

Common parameters are extra switches PowerShell adds to every advanced function. They control errors, previews, and confirmations in the same way for every command.

Stop turns a soft error into a catchable error

Get-Item cannot find the path. With -ErrorAction Stop, the catch block prints the message.

try { Get-Item "/definitely-not-here-xyz" -ErrorAction Stop } catch { "Caught: " + $_.Exception.Message }

Continue reports the error, SilentlyContinue hides it

Both lines keep running after the missing file. Merging the error stream into the result lets the count show how many errors each choice let through.

$seen = @(Get-Item "missing.txt" -ErrorAction Continue 2>&1)
"Continue errors: " + $seen.Count
$quiet = @(Get-Item "missing.txt" -ErrorAction SilentlyContinue 2>&1)
"Silent errors: " + $quiet.Count
"still running"

WhatIf previews a change without making it

The function asks PowerShell whether to proceed. With -WhatIf, PowerShell answers no, and the function prints its Skipped line.

function Remove-DemoFile {
    [CmdletBinding(SupportsShouldProcess)]
    param([string]$Path)
    if ($PSCmdlet.ShouldProcess($Path, "Remove file")) {
        Remove-Item -Path $Path
        "Removed: $Path"
    } else {
        "Skipped: $Path"
    }
}
Set-Content -Path "demo.txt" -Value "keep me"
Remove-DemoFile -Path "demo.txt" -WhatIf
Test-Path "demo.txt"

Confirm:$false skips the question

A high-impact change would ask first. Passing -Confirm:$false skips the question and lets the script run.

function Remove-DemoFile {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact="High")]
    param([string]$Path)
    if ($PSCmdlet.ShouldProcess($Path, "Remove file")) {
        Remove-Item -Path $Path
        "Removed: $Path"
    } else {
        "Skipped: $Path"
    }
}
Set-Content -Path "gone.txt" -Value "delete me"
Remove-DemoFile -Path "gone.txt" -Confirm:$false
Test-Path "gone.txt"

Example

function Remove-DemoFile {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact="High")]
    param([string]$Path)
    if ($PSCmdlet.ShouldProcess($Path, "Remove file")) {
        try { Remove-Item -Path $Path -ErrorAction Stop; "Removed: $Path" } catch { "Caught: " + $_.Exception.Message }
    } else {
        "Skipped: $Path"
    }
}
Set-Content -Path "demo.txt" -Value "keep me"
Remove-DemoFile -Path "demo.txt" -WhatIf
Test-Path "demo.txt"
Remove-DemoFile -Path "demo.txt" -Confirm:$false
Test-Path "demo.txt"

Key points

  • ErrorAction: -ErrorAction sets how a command reacts when something goes wrong.
  • Use -ErrorAction Stop to turn a soft error into a stopping error you can catch.
  • Use -ErrorAction SilentlyContinue to hide a soft error and keep going.
  • WhatIf: -WhatIf previews a change instead of making it.
  • Use -Confirm:$false to skip a confirmation question in a script.

In this example, the function first runs with -WhatIf, so it prints its Skipped line and the file stays. It then runs with -Confirm:$false, so it removes the file for real and the last check reports the file is gone.

Note

A script cannot answer a confirmation question, so it must pass -Confirm:$false for a change it means to make.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A script removes a file with a function that asks for confirmation. Which call runs without stopping for an answer?