windows – How to Empty User’s Downloads Folder at Login

active-directorywindowswindows-server-2019

Seems like a simple task but none of my solutions have worked.
Regular AD domain with regular users. We use terminal servers with non-roaming profiles so everything is stored on the TS disk. We want to empty their downloads folder at login because they never do it themselves and we're tired of having the disks fill up.

So, naturally I make a simple powershell script and create a GPO to run it at logon.
Problem is, the ps script required elevation, and logon scripts run at that user's permission level and cannot be elevated.

Here's the PS line:

Get-ChildItem C:\Users\*\Downloads\* | Remove-Item -Force

Works great if you manually run it from an elevated powershell instance.

Ok so no GPO. Next thought is to create a scheduled task on each terminal server and run with a service account that has admin privilege.

Problem there is that it doesn't work to empty each user's download folder… only the service account's.

So- I don't want to statically type each and every user's download folder path into the .ps1 file, because obviously that would be a management nightmare.

So does anybody know:

  1. A way to empty the download folder on user login through a GPO without running a script to do it.
    or
  2. A way to improve the script to recursively look through each user's folder, find the downloads folder, and empty it.. without having to specify the name of each user.

How can this easily be done?
Thanks

Best Answer

Your problem with that user logon script is that you were trying to delete ALL users' Download contents, if that's the command you were using.

You want to delete each specific user's Downloads, surely. So if Downloads is in their profile directory:

Get-ChildItem $env:USERPROFILE\downloads | Remove-Item -recurse

You may need to sign your script or set -ExecutionPolicy Bypass depending on the execution policy you have for Powershell. There's plenty of advice out there on how to run Powershell in a logon script.

If you're doing it in ye olde batch file, it's simply:

rd /s /q %USERPROFILE%\Downloads\*
del /q  %USERPROFILE%\Downloads\*.*