ConfirmPreference

If the Confirm parameter is not set, whether or not a prompt is shown is determined by PowerShell. The value of the ConfirmPreference variable is compared with the stated impact of a command.

By default, the value of ConfirmPreference is High, as shown in the following code:

PS> $ConfirmPreference
High

By default, commands have a medium impact.

Finding ConfirmImpact

In scripts and functions, the ConfirmImpact setting is part of the CmdletBinding attribute: [CmdletBinding(ConfirmImpact = 'High')].
If CmdletBinding or ConfirmImpact are not present, the impact is medium.

The impact of a function or cmdlet may be viewed using the ConfirmImpact property of a command's metadata:
New-Object System.Management.Automation.CommandMetadata(Get-Command Remove-Item).

ConfirmPreference has four possible values:

  • High: Prompts when command impact is High (default)
  • Medium: Prompts when command impact is Medium or High
  • Low: Prompts when command impact is Low, Medium, or High
  • None: Never prompts

A new value may be set by assigning it in the console; for example, it can be set to Low:

$ConfirmPreference = 'Low'  
ConfirmPreference and the Confirm parameter

While ConfirmPreference may be set to None to suppress confirmation prompts, confirmation may still be explicitly requested. Let's look at an example:
$ConfirmPreference = 'None'
New-Item NewFile.txt -Confirm

Since the Confirm parameter is supplied, the ConfirmPreference value within the scope of the command (New-Item) is Low, and therefore the prompt displays.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.20.56