Q: How to remove all items that haven’t been modified in X days using scheduled task without any supporting script files

Short answer:

Create a scheduled task and put the code below into the start a program step after updating the number of days directory path

powershell "Get-Childitem 'C:\Test Directory with old files' | Where LastWriteTime -le $(Get-Date).AddDays(-30) | Remove-Item"

 

Long Answer:

Open Task Scheduler and create a new basic task

image

Give it a name

image

 

Next > Next > Next until you get to the Start a Program action

Now enter the code below:

powershell "Get-Childitem 'C:\Test Directory with old files' | Where LastWriteTime -le $(Get-Date).AddDays(-30) | Remove-Item"

 

image 

Next and then Yes

image

Finish

image

Done, set whatever schedules, triggers, etc you want. The key is finally having a working one liner

Advertisements

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: