How do I automatically choose “No” when Remove-Item prompts for confirmation?

Question:

When using PowerShell’s Remove-Item to remove a directory that is not empty, it will prompt for confirmation:

If I run powershell in non-interactive mode, I get an error instead:

I know that I can use -Recurse to have Remove-Item proceed as if I had chosen the “Yes” option. Can I somehow proceed as if I had chosen the “No” option?

(Just for clarity: -Force and -Confirm:$false are not what I want here.)

Answer:

IMHO the correct answer is to set the -ErrorAction to SilentlyContinue. Then you don’t have to have empty try-catch code.

Like this:

Source:

How do I automatically choose “No” when Remove-Item prompts for confirmation? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply