1

I am trying to clean up folders, keeping the folders, but removing the files. I do not want to delete anything in some folders though and cannot figure out how. Here is what I have so far, but it deletes everything in each folder and also removes the 2 scripts on the first level. The ones I have marked with a red rectangle I need the script to ignore, the other folders I want the files deleted from.

Get-ChildItem -Path 'C:\Users\Tara.verity\Documents_Local\RemoveFilesFromFolders'  -Exclude "*.exe","*.config","assets", "Documentation" -Recurse | ForEach-Object {$_.Delete()}

enter image description here

taraloca
  • 9,077
  • 9
  • 44
  • 77

1 Answers1

0

Remove -Recurse from the Get-ChildItem call, pipe the results to another Get-ChildItem call to ensure that subdirectories among the item in the target folders have their items enumerated (without being included in the output themselves), and pipe the results to Remove-Item:

Get-ChildItem `
    -Path 'C:\Users\Tara.verity\Documents_Local\RemoveFilesFromFolders' `
    -Exclude *.exe, *.config, assets, Documentation |
  Get-ChildItem -Force |
  Remove-Item -Recurse -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf and re-execute once you're sure the operation will do what you want.

Note:

  • Even though your input path is a literal path and should therefore normally be passed via -LiteralPath, a bug in Windows PowerShell (since fixed in PowerShell (Core)) causes -Include and -Exclude to be ignored when combined with -LiteralPath

    • If you were to use -Include rather than -Exclude you'd have to additionally ensure that your -Path value ends in \* (or is just *), for the reasons explained in this answer.
  • Note that the enumeration via the second Get-ChildItem call includes any nested subdirectories, which are also removed by Remove-Item(the -Recurse switch prevents a confirmation prompt).

    • If you only want to remove the files from these directories, add -File to the second Get-ChildItem call.

    • If you want to remove all files from these directories recursively while retaining nested subdirectories, add -File -Recurse.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • @taraloca: D'oh! Please see my update, which fixes the logic while still using `-Exclude` logic. Glad to hear that the `-WhatIf` tip helped. – mklement0 Aug 10 '23 at 16:48
  • @taraloca, I only used [`Push-Location`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/push-location) and [`Pop-Location`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/pop-location) when I thought it was necessary to provide all the paths to match as part of the `-Path` argument, to avoid repetition. With the `-Exclude`-based solution, this is no longer necessary, I removed them. In general, changing the current location in scripts is best avoided, because it affects the session globally. – mklement0 Aug 10 '23 at 22:59