0

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
TallyHawk
  • 3
  • 3
  • In order to avoid a confirmation prompt when using `Remove-Item` to remove a _non-empty directory_, use `-Recurse`; add `-Force` to additionally ensure that removal succeeds if the directory happens to contain _hidden_ items. `-Confirm:$false` _in addition_ is only needed if the `$ConfirmPreference` preference variable's value was changed from its default (`'High'`). See the linked duplicate for details. – mklement0 Oct 28 '22 at 22:18

2 Answers2

0

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.

RetiredGeek
  • 2,980
  • 1
  • 7
  • 21
0

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.

Toni
  • 1,738
  • 1
  • 3
  • 11