Powershell One-liners – Folder cleaning

Some application just log to much, and the log files tend to get huge.

Or maybe you have a situation where you just don’t need the log’s from the last 2 years, but would like to retain a few days back, in case of error:

Windows Scheduled task and Powershell to the rescue


powershell.exe -command "& {get-childitem -Path e:\IIS_Logs -Recurse| %{if ($_.CreationTime -lt ([DateTime]::Now.AddDays(-5))){remove-item $_.FullName}}}"

This will delete files older than 5 days in the targeted folder (IIS_logs) and all subfolders

I have tried to wrap this in a schtasks command, so far been unsuccessful.

In the Windows Server 2008 R2 GUI:

  • Task Scheduler
  • Create a basic task
  • Give it a name and a description
  • Choose the Task trigger and the properties for the selected trigger
  • Choose the action (start a program)
  • Add the path for powershell.exe in the program/script box and everything following to the Add arguments (optional) box
  • image
  • Review and Finish.