Automatic confirmation of deletion in powershell
Asked Answered
C

7

47

I'm running the following command:

get-childitem C:\temp\ -exclude *.svn-base,".svn" -recurse | foreach ($_) {remove-item $_.fullname}

Which prompts me very frequently like this:

Confirm
The item at C:\temp\f\a\d has children and the Recurse parameter was not specified. If you continue,
all children will be removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): 

How can I have it automatically set to "A"?

Coco answered 20/1, 2011 at 23:8 Comment(0)
L
29

Try using the -Force parameter on Remove-Item.

Lite answered 21/1, 2011 at 0:3 Comment(1)
please see below answers for a more comprehensive answer to this problem.Titfer
P
72

The default is: no prompt.

You can enable it with -Confirm or disable it with -Confirm:$false

However, it will still prompt, when the target:

  • is a directory
  • and it is not empty
  • and the -Recurse parameter is not specified.

-Force is required to also remove hidden and read-only items etc.

To sum it up:

Remove-Item -Recurse -Force -Confirm:$false

...should cover all scenarios.

Patrizius answered 13/2, 2014 at 14:21 Comment(2)
So if you want it to delete but not delete, and just raise an error when it's a directory you also have to test for it being a directory separately.Laughton
@WarrenP What behavior exactly are you looking for? If you tell PowerShell to delete a directory, it will delete it. Unless it's not empty, then the behavior will be as described.Patrizius
C
35

Add -confirm:$false to suppress confirmation.

Conti answered 21/1, 2011 at 0:16 Comment(2)
This WILL NOT work. -Confirm is for forcing confirmation. Having it off just runs the normal decision logic.Spikelet
do you use -force -confirm:$false ?Escape
L
29

Try using the -Force parameter on Remove-Item.

Lite answered 21/1, 2011 at 0:3 Comment(1)
please see below answers for a more comprehensive answer to this problem.Titfer
L
11

Add -recurse after the remove-item, also the -force parameter helps remove hidden files e.g.:

gci C:\temp\ -exclude *.svn-base,".svn" -recurse | %{ri $_ -force -recurse}

Linctus answered 14/6, 2012 at 7:4 Comment(1)
-recurse prevents confirmation for removing non empty folders. Thanks.Edmondo
F
7
Remove-Item .\foldertodelete -Force -Recurse
Flaw answered 11/7, 2016 at 16:7 Comment(0)
S
1

Just an additional tip: Let's say the PS1 (helloworld.ps1) has a code like below:

Set-ExecutionPolicy Unrestricted -Confirm:$false -Force
Write-Host "Hello, World!"

And if we expect that each time the code runs, it would automatically run the Set-ExecutionPolicy without prompting the user & run the code silently...

It won't work that way!! I'm still figuring out how to run a PS code without prompting the user & will post the solution if I find out

Shipping answered 29/6, 2022 at 4:26 Comment(0)
B
0

You just need to add a /A behind the line.

Example:

get-childitem C:\temp\ -exclude *.svn-base,".svn" -recurse | foreach ($_) {remove-item $_.fullname} /a
Baerman answered 15/7, 2015 at 3:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.