2

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!

1
  • 1
    Add -recurse to your remove-item. Commented Mar 18, 2019 at 22:11

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.