Home > database >  Deleting folders with Powershell recursively issue
Deleting folders with Powershell recursively issue

Time:01-06

I need to delete some subfolders under a folder 'ToDelete'. I am using this to do that: (both should do the same deletion). my problem is that there is other folder called 'Do_Not_Copy' under ToDelete folder that contain also a folder called 'Tools' that should not be deleted. how I can protect this 'Tools' subfolder? -Exclude doesn't work. My workaround for now is to use Rename-Item for the Tools folder

$DestinationFolder = "\\google\server\ToDelete"

Remove-Item -Path $DestinationFolder\  -Include "Tools", "Support", "Installer", "GUI", "Filer", "Documentation" -Recurse -Exclude "Do_not_copy\SI\Tools" -Force 
Get-ChildItem $DestinationFolder\ -Include "Tools", "Support", "Installer", "GUI", "Filer", "Documentation" -Recurse -Force | ForEach-Object { Remove-Item -Path $_.FullName -Recurse -Force }

CodePudding user response:

The -Recurse switch does not work properly on Remove-Item (it will try to delete folders before all the subfolders in the folder have been deleted).
Sorting the fullnames in descending order by length ensures than no folder is deleted before all the child items in the folder have been deleted.

Try

$rootFolder = '\\google\server\ToDelete'
# create a regex of the folders to exclude
$Exclude = 'Do_not_copy\SI\Tools'  # this can be an array of items to exclude or a single item
# each item will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($Exclude | ForEach-Object { [Regex]::Escape($_) }) -join '|'
# the array of folder names (not full paths) to delete
$justThese = 'Tools', 'Support', 'Installer', 'GUI', 'Filer', 'Documentation'

(Get-ChildItem -Path $rootFolder -Directory -Recurse -Include $justThese).FullName |
    Where-Object { $_ -notmatch $notThese } |
    Sort-Object Length -Descending |
    Remove-Item -Recurse -Confirm:$false -Force -WhatIf

As usual, I have added the -WhatIf safety switch, so no folders will be deleted and in the console you can see what would happen. When satisfied the correct folders will be removed, comment out or remove that -WhatIf switch and run again

  • Related