Clear printer cache folder

By | 12th May 2021

PowerShell script, which clear printer cache folder once a week. Recommend to add this script to Windows Task Scheduler.

Main features:

  • the script is executed without the user’s intervention
  • works very quickly at the background
  • empty recycle bin after every operation
  • “if statement” to prevent a crash
<#
Clear cache script for selected folder.
Requirements: PowerShell version >= 5
in case a script doesn't run: Set-ExecutionPolicy RemoteSigned
set back: Set-ExecutionPolicy Restricted
#>

$folder_link = "C:\Windows\System32\config\systemprofile\TOSHIBA\eSTUDIOX\UNIDRV\cache"

#  Set a amount of days you would like to keep the cache.
$days_back = 5

#  If a folder contains minimum 1 item: run a script.
if((Get-ChildItem $folder_link -force | Select-Object -First 1 | Measure-Object).Count -ne 0)
    {
        Get-ChildItem –Path $folder_link -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-$days_back))} | Remove-Item
        Clear-RecycleBin -DriveLetter C -Force  # Empty recycle bin on C drive.
    }

#  Otherwise: exit.
else
    {
        Write-Host "Folder Empty"
        Exit
    }