PowerShell script to delete all files but leaves directories intact

In the category of scripts to clean up computers, here is a one-liner for removing all files but leaves the directory structure intact.

I found this to be very useful for companies that have a network share where every user has it’s own directory but company policy states that all files will be deleted from that share in the weekend.

ls -recurse | where {!@(ls -force $_.fullname)} | rm –whatif

Big fat note: Executing this on a drive will remove all files… also on the system disk if executed on that drive!!!

Leave a Reply

Your email address will not be published. Required fields are marked *