PowerShell script to delete files & folders

I needed a script to check the date of files and folders in a sub folder on a Windows server and could not find the right solution with a command or VB script, however a Powershell script did exactly what I needed.

I’m sure there is a VB script out there to do this, but I couldn’t really find one and I just don’t have the time or skills to create one – so this PS script works perfectly for my needs:
__________________________________________________________
$filelocation = “I:DataBUBackupVMs”
gci -path $filelocation -r | where-object {$_.PsIsContainer -eq “True”} |
?{$_.creationtime -lt (get-date).adddays(-7)} | %{del $_.fullname -r -force}
__________________________________________________________

The location of my folders and data is on a I: drive under the DataBU folder and anything older than 7 days will be deleted e.g. (get-date).adddays(-7). The -r will perform a recursive delete and the -force will ensure the data is deleted regardless of it’s access rights (assuming the script operator has enough permission).

Bookmark the permalink.