This short script helps to create a scheduled task that can delete files older than specified days;

In below example, a new scheduled task named "Clear IIS Logs" that deletes .log files older than 30 days under c:\inetpub\logs is created.

It also set to run as local administrators with highest privileges. And schedule triggers at every day 4:00 AM.


$action = New-ScheduledTaskAction -Execute 'forfiles.exe' -argument '-p "C:\inetpub\logs" -m *.log* /D -30 /C "cmd /r del @path /s"' 
$settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel 
$trigger =  New-ScheduledTaskTrigger -Daily -At 4am 
$Principal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel "Highest" 
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $Principal -TaskName "Clear IIS Logs" -Description "Clear IIS Logs older than 30 Days"