Miguel Vega

Quick Tip - Recursively Delete Files

Ever had to delete files from a directory where the files' last write time is older than a specific date? If so, the following snippet might come in handy:

$path = "\\server\folder"

$olderThan = (Get-Date).AddDays(-1)

Get-ChildItem -Path $path -Recurse | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $olderThan } | Remove-Item -Force

The above code will recursively delete all files where the "LastWriteTime" is older than the date specified.

If you are as paranoid as I am, feel free to append the "-WhatIf" switch at the end of the command to display all the files that would be deleted without actually deleting them.

Copyright © 2020 - Design by FS