Reputation: 93
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
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:
-Confirm
or -Confirm:$False
, nor the value of the $ConfirmPreference
preference variable impact whether the extra prompt is shown.-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