Schedule task in Windows to delete files from specific folder once it reaches a date

I need to create a scheduled task to delete the contents of a specific folder everyday.  I need help with the scrip to do this and the steps on windows server to accomplish this  Thanks in advance for the help.
Who is Participating?
cut and paste this to as myscript.bat

echo off
set myFolder="c:\temp"

del /q /s %MyFolder%\*.*

then go here for directions on the scheduled task:
step 3 of the link is where you'll browse to this .bat file you just created
Batch is so ugly :P

PowerShell variation below.

Put this into a .ps1 file:

$Folder = "C:\TEMP"
$CutOffDate = (Get-Date).AddDays(-1)

$Items = Get-ChildItem $Folder | ? {$_.PSIsContainer -eq $False}

foreach ($Item in $Items)
	if ($Item.LastWriteTime -lt $CutOffDate)

Open in new window

Then put this into a .BAT file, replacing the file path with the one you saved above:

PowerShell.exe -Command "& {C:\Folder\yourfile.ps1}"

Open in new window

Then add a new scheduled task and tell it to run the batch file for the task. I'm assuming you have PowerShell installed.

You may need to enable the right execution policy to exec .ps1 scripts which you can do by running the command from this URL

Command is Set-ExecutionPolicy RemoteSigned from a Powershell.exe prompt which has been run as admin if you are lazy :)

TonyRebaAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.