How do I remove-items in a folder without the confirm box popping up?

398 Views Asked by At

I want to deleted all the files in the temp folder... any .zip files , .txt files and any folder files including whatever is inside each of those folders (everything). I thought this would be simple but so far my script keeps getting the confirmation pop-up asking if I want to delete all these child items. I tried using -confirm:$false but that doesn't seem to work. I appreciate any suggestions. Thank you.

$list = Get-ChildItem -directory "C:\temp\*" -Include * -Name 
get-childitem c:\temp -Include @(get-content $list) | Remove-Item -Force -whatif

I tried using the -confirm:$false argument as well as the -force with no luck.

2

There are 2 best solutions below

1
RetiredGeek On BEST ANSWER

You want -path vs -directory.

#Looking for Temp under Windows:
$list = (Get-ChildItem -path "C:\*\Temp*\*.*").FullName | Remove-Item -Force

#Looking for Temp under Root:
$list = (Get-ChildItem -path "C:\Temp*\*.*").FullName | Remove-Item -Force

If your Temp dirs have subs you could also add the -recurse switch.

0
Toni On

To avoid confirmation requests add the parameter -confirm:$false. If you want to include everything don't specify the parameter -include * - the default is to return everything, no need to slow down due to unecessary filters.

just do:

get-childitem -path C:\temp -directory | remove-item -force -confirm:$false -recurse

btw. u did specify the parameter -directory, currently only directories are returned by get-childitem, so files directly stored in C:\temp would remain.