# PowerShell: Delete folders based on name

G30th September 2015

Use this simple script to delete any folders in a directory (or its subdirectories) that contain a particular string or strings.

I have a lot of folders which contain legacy FrontPage extensions (in the format “_vti_cnf”). These are of no use and are simply taking up space on my hard drive.

Whilst I could trawl through every directory and subdirectory and manually delete each and every instance of a _vti_cnf folder or file, this process is much faster when PowerShell is employed. The following script will delete folder (as well as subfolders and files) based on a string or a series of strings. In this example, it will delete any folder or file that contains “_vti_cnf” or “.svn”.

$source="C:\path\to\files" #location of directory to search$strings=@("*vti_cnf", "*.svn")

cd ($source); get-childitem -Include ($strings) -Recurse -force | Remove-Item -Force –Recurse

There are just two parameters that need to be specified:

• $source = The folder in which to start the search • $strings = The search strings that will be used to decide if folders should be deleted

As with all scripts of this nature, caution must be employed. Always run the script with a -whatif command first to see what will happen when the script is run.