23. januar 2023

Powershell - Delete files older than "$Days" days

#---- Delete files in $Path older than $Days ----#
$Path = "C:\Temp"
$Days = "30"
dir $Path -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt $Days } | Remove-Item -Force -WhatIf

Recursive delete :
dir $Path -recurse -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt $Days } | Remove-Item -Recurse -Force -WhatIf


Remark : Parameter "-WhatIf" is there only for testing, remove when sure it deletes right files
TEST IN Powershell ISE before deleting wrong files

Ingen kommentarer:

Send en kommentar

Bemærk! Kun medlemmer af denne blog kan sende kommentarer.