I create a PowerShell command that have to remove on my diskstation all files inside folders which contains the word "rendery". Everything would be fine, if the command deleted the files in the folder containing the word "rendery", because the script currently deletes the folder (along with the files that are inside) that contains the word "rendery".
Get-ChildItem C:\Test -Recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "rendery"} | Remove-Item -Recurse -Force
CodePudding user response:
Insert another Get-ChildItem
call after Where-Object
to resolve only the child items of the target folders:
Get-ChildItem C:\Test -Recurse | Where-Object {$_.PSIsContainer -and $_.Name -match "rendery"} | Get-ChildItem | Remove-Item -Recurse -Force
Note that $_.PSIsContainer
is already a boolean value, so you can shorten $_.PSIsContainer -eq $true
to just $_.PSIsContainer