Confirm parameter

The Confirm parameter causes a command to prompt before an action is taken; for example, the Confirm parameter forces Remove-Item to prompt when a file is to be removed:

PS> Set-Location $env:TEMP
PS> New-Item IMadeThisUp.txt -Force
PS> Remove-Item .IMadeThisUp.txt -Confirm

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:UserswhoamiAppDataLocalTempIMadeThisUp.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

We have seen that a confirmation prompt may be forcefully requested in the previous example. In a similar manner, confirmation prompts may be suppressed; for example, the value of the Confirm parameter may be explicitly set to false, as shown in the following code:

Remove-Item .IMadeThisUp.txt -Confirm:$false
There is more than one way of prompting

There are two ways of requesting confirmation in PowerShell: Confirm and the associated ConfirmPreference; the variable only acts against one of these.
Using the parameter or changing the variable will not suppress all prompts. For example, Remove-Item will always prompt if you attempt to delete a directory that is not empty without supplying the Recurse parameter.

This technique is useful for commands that prompt by default; for example, Clear-RecycleBin will prompt by default:

PS> Clear-RecycleBin

Confirm
Are you sure you want to perform this action?
Performing the operation "Clear-RecycleBin" on target " All of the contents of the Recycle Bin".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

Setting the Confirm parameter to false for Clear-RecycleBin will bypass the prompt and immediately empty the recycle bin:

Clear-RecycleBin -Confirm:$false  
Finding commands with a specific impact

The following snippet will return a list of all commands that state they have a high impact:
Get-Command -CommandType Cmdlet, Function | Where-Object {
    $metadata = New-Object System.Management.Automation.CommandMetadata($_)
    $metadata.ConfirmImpact -eq 'High'
}
..................Content has been hidden....................

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