Powershell script to delete old files

Posted by yantwill on Stack Overflow See other posts from Stack Overflow or by yantwill
Published on 2009-05-06T02:15:48Z Indexed on 2010/05/04 0:48 UTC
Read the original article Hit count: 329

Filed under:
|
|
|

The following script will delete files in a named directory that are older than 14 days and write to a .txt with the path and the files deleted (found this script on another forum..credit to 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:

  1. What is -lt and what is -le and what is -gt? When would I use each one
  2. The script above only deletes file...how can I delete folders as well?
  3. The script above is based off of LastWriteTime .. what about CreatedDate or LastAccessed time?

© Stack Overflow or respective owner

Related posts about powershell

Related posts about c#