Home > one-liner, Powershell > Powershell One-liners – Folder cleaning

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.
    /theadminguy
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: