Powershell script to remove Sage temp filesPosted on 19 July 2018 by Beaming Support
If you host Sage files on a file server, then you may have noticed that the most recent versions of Sage leave temp files in the C:WindowsTemp folder. This has occurred in at least the latest two versions of Sage Accounts and will eat up a lot of storage space over time.
If you value your C drive space, then the script below can be run once a week (on a weekend). The script will find and remove all files created within the month and named using the Sage standard, which are located within the C:WindowsTemp folder.
Open Notepad and paste in the following:
#Set $month as current month, 2 digit string
$Month = Get-Date -UFormat %m
#Sets $Year variable to current year, 4 digit string
$Year = Get-Date –Uformat %Y
#Sets $Sage variable as the file name needed, 2018-06*
$Sage = $Year + “-” + $month + “*”
#Retrieves a list of folders and removes them,
Get-ChildItem -Path “C:WindowsTemp” -Filter $Sage | Remove-Item –Recurse
Save this file as ‘SageRemoval.ps1’ in a folder that is easy to find (C:Script for example).
Then open notepad again and write in the following:
Powershell.exe -executionpolicy remotesigned -file C:ScriptSageRemoval.ps1
Save this file in the same location for easiness and call it ‘SageRemove.bat’.
Open Task scheduler and create a new basic task. Name it ‘Sage file removal’ and tick ‘Run whether user is logged on or not’. Create a new trigger, set Weekly and then choose the next Sunday at a reasonable time. Set the task to recur every Sunday, hit enable and OK.
Under actions, choose New, then Browse to the folder that you saved the bat file in. Leave arguments and start in as blank and press OK. Press Ok to the task and enter the domain credentials to save the task.
That’s it. Now once a week the Sage temp files will be automatically removed and your C drive won’t fill up causing you a headache.