Using PowerShell Default Parameter Values to Prevent Unfortunate Accidents

If you have ever encountered the unfortunate circumstance of accidentally shutting down or rebooting your computer with PowerShell I am here to give you a tip you will definitely want to remember. First of all this solution requires version 3 so if you are still on version 2 then you will need to stash this one away.

The PowerShell cmdlets for shutting down and rebooting a computer are Stop-Computer and Restart-Computer respectively. By default these cmdlets don’t prompt for confirmation so if you accidentally enter them your system is going to go down. To prevent this you can use the default parameter values to set the default for these cmdlets to always confirm.

I am not going to explain how default parameter values work in PowerShell. Instead I am going point you to use the builtin and very descriptive help topic built in to PowerShell. Read all about it by executing help about_Parameters_Default_Values.

[codesyntax lang=”powershell”]

$PSDefaultParameterValues = @{

    "Restart-Computer:Confirm" = $True
    "Stop-Computer:Confirm" = $True
}[/codesyntax]