How can I recursively delete folder with a specific name with PowerShell?
I tend to avoid the -Include
parameter on Get-ChildItem
as it is slower than -Filter
. However in this instance (since it can't be expressed as a -Filter
), this is what I would use:
Get-ChildItem . -Include .svn,_svn -Recurse -Force | Remove-Item -Recurse -Force
or if typing this at the prompt:
ls . -inc .svn,_svn -r -fo | ri -r -fo
This one should do it:
get-childitem -Include .svn -Recurse -force | Remove-Item -Force -Recurse
Other version:
$fso = New-Object -com "Scripting.FileSystemObject"
$folder = $fso.GetFolder("C:\Test\")
foreach ($subfolder in $folder.SubFolders)
{
If ($subfolder.Name -like "*.svn")
{
remove-item $subfolder.Path -Verbose
}
}
For fast deletion, use -Filter
To recursively delete a folder with a specific name -Filter
is significantly faster than -Include
.
So, for visibility, here is the faster version of @Keith's answer:
Fully typed-out:
Get-ChildItem -Filter .svn -Recurse -Force | Remove-Item -Recurse -Force
Short version:
ls . -filter .svn -r -fo | ri -r -fo