Dude be PSing
Dude be PSing

Reputation: 93

Powershell delete folders -force

I cannot seem to remove files/folders without me having to enter [A] for all. What am I missing?

Get-Childitem "C:\Users\*\AppData\Local\Temp\*" -ErrorAction SilentlyContinue | 
  Where {$_.CreationTime -lt (get-date).adddays(-7)} |
    Remove-Item -Verbose -ErrorAction SilentlyContinue -Confirm:$false -Force

Thanks!

Upvotes: 2

Views: 2409

Answers (1)

mklement0
mklement0

Reputation: 440431

As TheMadTechnician points out, it is the -Recurse switch that is needed to suppress the extra confirmation prompt that is presented when Remove-Item is called to remove a nonempty container (directory).

This extra confirmation prompt is specifically presented for the higher-risk operation of removing containers along with their children and is independent of the common confirmation mechanism:

  • That is, neither the cmdlet's declared impact level, nor the presence of -Confirm or -Confirm:$False, nor the value of the $ConfirmPreference preference variable impact whether the extra prompt is shown.
  • These elements do, however, as usual, control whether the common prompt is shown, so that if you pass
    -Confirm, for instance, you'll get the common confirmation prompt in addition to the extra one, after the latter; in the case of a non-container item (file) or a container that happens to be empty, -Confirm will show only the common prompt.

Note:

Typically, such extra prompts are suppressed with the -Force switch, as in the case of the
Set-ExecutionPolicy cmdlet.

In the context of Remove-Item, however, -Force has a different meaning: it ensures that files or directories that are hidden or files that have the read-only attribute set can be deleted.

Therefore, it is -Recurse that signals the explicit intent to remove a container along with its content, and thereby suppresses the extra prompt.

Upvotes: 2

Related Questions