PowerShell Script that deletes files which starts with a specific string and older than specific days

Here is the powershell script named deleteTempPhpFiles.ps1. I placed that file D:\DeleteTempPHP

 

$Path="C:\Windows\Temp"

$Files="php*.*"

Get-ChildItem $Path -Include $Files -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-15))} | Remove-Item

 

 

You can run the script with a schedule task, Under the Actions Tab of Task Scheduler, make sure you define the following fields as follows

Action: Start s program

Program/Script: powershell.exe

Add Arguements: -ExecutionPolicy Bypass -file "D:\DeleteTempPHP\deleteTempPhpFiles.ps1"