powershellconfirm

How I can set automatically [L] Not to all?


My question is to powershell 7.2 switch [L] Not to all

I found information about -confirm but not a help how I can set automatic [L] 'Not to all' into a function. I will that not a user make a input. -Confirm:$false, ConfirmPreference = "None" or 'Set-ExecutionPolicy Unrestricted' is not a help. The query comes every time. Do you have an idea / example how to set [L] to automatic?

confirm question


Solution

  • PowerShell's confirmation prompts aren't designed to be responded to programmatically; instead:


    In your particular case, the equivalent of defaulting to [L] No to All would be to not invoke Remove-Item at all if the target directory is non-empty:

    # Assume $dir contains the literal directory path of interest.
    # Note that -Force in the case of Get-ChildItem causes inclusion of hidden items.
    if (Get-ChildItem -Force -LiteralPath $dir | Select-Object -First 1) {
      Write-Error "Cannot remove directory $dir, because it is not empty."
    } 
    else {
      Remove-Item -LiteralPath $dir
    }
    

    Conversely, as stated, Remove-Item -Recurse -Confirm:$false -Force -LiteralPath $dir would suppress the confirmation prompt, but quietly go ahead with the deletion ([A] Yes to All is implied).