Powershell script to delete old files

The following script will delete files in the named directory that are older than 14 days, and write in .txtwith the path and deleted files (find this script in another forum..credit for shay)

dir c:\tmp -recurse | where {!$_.PsIsContainer -AND $_.lastWriteTime -lt (Get-Date).AddDays(-14) } | select LastWriteTime,@{n="Path";e={convert-path $_.PSPath}} | tee c:\oldFiles.txt | Remove-Item -force -whatif

I have 3 questions:

  • What is -ltand what is -leand what is -gt? When will I use each
  • The script above only deletes the file ... how can I delete folders?
  • The top of the script is based on LastWriteTime.. how about time CreatedDateor LastAccessed?
+5
source share
2 answers

Ok, here we go:

+9

-lt -le -gt - <, lt; =, > . " help about_Comparison_Operators" powershell , .

, ! $. PsIsContainer AND. , .

, " " "" LastAccessed ", , LastWriteTime, , .

+3

All Articles