Saturday, May 5, 2018

Check the Confirmation Preference in PowerShell

All PowerShell cmdlets have an impact on a computer. Of course, some cmdlets have a bigger impact than others. A simple Get-* cmdlet typically just retrieves information as Read-Only, whereas a Remove-* cmdlet could bring a system to its knees.

The impact that a cmdlet can have is hard-coded into the cmdlet itself by the developer of the cmdlet, be it Microsoft or a 3rd-party developer. The developer determines if the impact is “High”, “Medium”, or “Low”. For example, removing a mailbox in Exchange is considered a HIGH impact, and Stop-Process is considered a MEDIUM impact.

But our PowerShell environment also has a ‘default’ impact setting:

                $ConfirmPreference



So when we enter a command into PowerShell, it compares the impact level of the cmdlet with our default setting. If the cmdlet’s impact level is greater than our $confirmpreference setting, then the cmdlet prompts us, asking, “Are you sure you want to do this?” Since our system’s set for “HIGH”, then all cmdlets will run without any prompt or confirmation. While being prompted after running cmdlets might be annoying, it may be wise for a PowerShell beginner to take precautions.

For example:

Here I launched an instance of calculator.exe, then grabbed the ID using ‘Get-Process.’ When I entered ‘Stop-Process <ID#>’, PowerShell compared my $confirmpreference setting (HIGH) with the cmdlet’s Impact setting (MEDIUM) and decided that I must know what I’m doing, so it killed the instance of calculator.exe without hesitation. But if I’m feeling skittish, I’ll might want to lower my $confirmpreference setting to a safer level:

                $ConfirmPreference = “low”



Let’s see what happens when I try to kill a new instance of calculator.exe:


Now PowerShell prompts me before doing anything with an impact higher than LOW.

No comments:

Post a Comment