Deleting Files With PowerShell
- 0 Comments
I have an application at work that generates files and stores them in a particular folder structure. Nested folders. Tens of thousands of them. The upside is that it’s generally efficient in calling these generated files. The downside is that they hog massive amounts of space over time. Even two weeks worth of files turns into 60 or 70 GB of data.
Windows Sucks At Deleting Files
Truly it does. It’s like the Dyson Vacuum; it has that much suction. My app at the office laughs at “erase” and “del.” I’m sure it gets a real chuckle when I try to delete these folders and files from the disk.
But…Here’s another way that PowerShell shines.
c:> PS $folderToDelete C:\myfolder\of_crap
c:> PS Remove-Item $folderToDelete -recursive
You will get prompted if you want to delete the specified directory and content. Just hit “Y” and then Enter. It will start to erase all the folders and files in the specified directory.
The “-recursive” tells PowerShell to delete all of the files and folders inside of $folderToDelete.
If you need to you can exclude a particular file you can use the “-exclude” switch or include a specific file or type with the “-include” command. Pretty nice, eh?






