GPO / Logon Script to clear temp files?


(Dan) #1

I am looking for a way to clean up users temp files and I have found a PS script that works well, but when I run it locally, I do get errors for the files it can not delete (locked).

Is there a way to add this to GPO to run for all users and will it return the errors or can those be suppressed?

$current_user = [Environment]::UserName
Get-ChildItem -Path “C:\Users$current_user\AppData\Local\Temp” | Remove-Item -Recurse


(Edwin Eekelaers) #2

@dan Try this.

$current_user = [Environment]::UserName
Get-ChildItem -Path "C:\Users\$current_user\AppData\Local\Temp\*" -recurse -erroraction silentlycontinue  | Remove-Item 

Small mod. The recurse should be before the pipe. Noten the different path bit too


(Dan) #3

Thanks - I am now getting a question"
The item at ___has children and the Recurse parameter was not specified. Y or N to continue

I tried using a -Force in various places and that did nothing


(Dan) #4

This worked:

$current_user = [Environment]::UserName
Get-ChildItem -Path “C:\Users$current_user\AppData\Local\Temp*” -recurse | Remove-Item -erroraction silentlycontinue -Confirm:$false


(Edwin Eekelaers) #5

Erroraction is not really required on the remove item if it’s present on the gci…
Next time try $env:username instead of [Environment]::UserName


(Dan) #6

Ok, this am, my “fixed” script is askign questions again, and 'm not sure how to edit your suggestion. If I just change:
$env:username instead of [Environment]::UserName
the script runs and deletes nothing.


(Edwin Eekelaers) #7

Try it step by step. First the user variable and then the gci without the pipe and see wat happens. There may be a typo somewhere.


(Dan) #8

It seems I had some commands in the wrong place. It works when I run it manually from my pc.


#9

Dan,

To answer the other part of your question, yes, once you save your script as a .ps1 you can add this to a GPO to run against all users just as though you created a .vbs script. If you need instructions on this, let me know.