0

Using Windows Server 2012 R2:

My application creates many temp files in a service account's temp folder:

C:\Users\SVC_ACCT\AppData\Local\Temp\

I need to build a PowerShell script that cleans the content of the temp folder every night, outside of busy hours.

Before I build the script, I wonder if there is such command line that I can reliably schedule it every night?

Allan Xu
  • 685
  • 2
  • 6
  • 12

2 Answers2

0

Built-in app, yes and no, as powershell is now built-in, and is the reference to create such task. (use a -WhatIf on the line to test it before)

Remove-Item -path c:\windows\temp\* 
yagmoth555
  • 16,758
  • 4
  • 29
  • 50
  • I wish it was that easy. There user and system temp locations. you should not hard code the location. You need to handle the files that are locked and can't be deleted. That is why I like to know if there is any built-in command line option other than custom script or custom code. – Allan Xu Aug 05 '16 at 14:03
  • No built-in, but like on my side I used profilenurse to clean a lot of user location, as it's configurable where it delete. It's maybe a better solution for you then than powershell.(https://www.sepago.com/blog/2014/01/22/freeware-profile-nurse-tool) – yagmoth555 Aug 05 '16 at 14:05
0

With environment variables you can get temp directory of the user running the script:

Remove-Item -path ${ENV:TEMP}

Or, you can call CleanMgr.exe which can also clean up other things. This will need a wrapper script: Automate process of Disk Cleanup cleanmgr.exe without user intervention

John Mahowald
  • 32,050
  • 2
  • 19
  • 34