5

I have a powershell script in which I have implemented the should process method with a high ConfirmImpact to ensure a prompt occurs.

Prompting is working as expected however the default response when nothing is entered is "Y"

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

Is there a way to change the default response to "N"? I would like the user to have to explicitly enter Y if they want to apply the script. I've written a pretty heavy handed script and last thing I want is for the script user to just hit enter without verifying what they are applying it to.

I've read the documentation on it and there is no mention of this anywhere.

Jessie
  • 284
  • 1
  • 3
  • 12

2 Answers2

3

After further research I have found a way to do this. In order to have a default response set to NO, ShouldContinue Method should be used instead of ShouldProcess.

Reading the documentation for ShouldContinue has a section in the format

ShouldContinue(String, String, Boolean, Boolean, Boolean)

The first boolean is to reference a security impact. True if the operation being confirmed has a security impact. If specified, the default option selected in the selection menu is 'No'.

Jessie
  • 284
  • 1
  • 3
  • 12
0

No, unfortunately not

With the current implementation of ShouldProcess(), the default option is always the first choice


Whether or not ShouldProcess() prompts the user to confirm the operation depends on whether the $ConfirmPreference automatic variable has a value higher than the ConfirmImpact attribute of the cmdlet being called.

$ConfirmPreference defaults to High, the highest severity impact level available.

To always void confirmation prompts for ShouldProcess(), set $ConfirmPreference to None:

# Define a function with ConfirmImpact Medium or higher
function f {
  [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
  param()

  if($PSCmdlet.ShouldProcess("Dangerous operation")) {
    Write-Host 'Welcome to the Danger Zone' -ForegroundColor Red
  }
}

# Set $ConfirmPreference to `Medium` to prompt for confirmation
$ConfirmPreference = 'Medium'

# Call our function - you'll be prompted for confirmation
f

# Set $ConfirmPreference to `None` to suppress confirmation prompts
$ConfirmPreference = 'None'

# Call our function - you won't be prompted for confirmation
f
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • I do not want to void confirmation prompts. I want the prompt to always come up, hence why my function is set to confirmationimpact High. My question involves in the actual response. If you just hit enter, the default response is Y. I want the user to have to enter Y and if nothing is entered, to assume a response of N. – Jessie Feb 20 '19 at 15:57
  • @Jessie Sorry, should have re-read your question, answer updated – Mathias R. Jessen Feb 20 '19 at 16:13
  • I was afraid this might be the case. This is pretty dangerous to default a yes response. I was hoping I could use the built in confirmation to take advantage of the whatif parameter as well. Guess I'll have to build my own prompt. – Jessie Feb 20 '19 at 16:17
  • It is, indeed. Might be worth filing an issue on GitHub for this to be overridable in future versions of PowerShell – Mathias R. Jessen Feb 20 '19 at 16:18
  • 1
    I am surprised no one has brought this up before considering the version of PowerShell released. I don't have a GitHub account myself. This will be the perfect time to create it then. – Jessie Feb 20 '19 at 16:22
  • Found the answer. Use ShouldContinue instead of ShouldProcess – Jessie Feb 20 '19 at 17:13