I can delete files with specific extensions in multiple folders with this:
Get-childitem * -include *.scc -recurse | remove-item
But I also need to delete folders with a specific name - in particular those that subversion creates (".svn" or "_svn") when you pull down files from a subversion repo.
In the PowerShell console, type Remove-Item –path c:\testfolder –recurse and press Enter, replacing c:\testfolder with the full path to the folder you want to delete.
Using PowerShell to Delete All Files Recursively If you need to also delete the files inside every sub-directory, you need to add the -Recurse switch to the Get-ChildItem cmdlet to get all files recursively.
Use the Delete() Method Every object in PowerShell has a Delete() method and you can use it to remove that object. To delete files and folders, use the Get-ChildItem command and use the Delete() method on the output.
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
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 } }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With