In this guide, I’m going to show you how to delete the contents of a users’ Downloads folder using a simple Powershell script. This script is especially useful to use on a Windows Server 2019 Terminal Server or rdweb host server.

Understandably, running a script across all users on a server can be scary, so I’ll show you how to edit the script to run it on 1 user first to ensure it works correctly before deploying it to all terminal server users.

Once you verify the C:\Users\username\Downloads folder is empty, you can use Task Scheduler to automate the process and set it to run daily, nightly, weekly, or at whatever interval you’d like.

At the end of this post, I also have a Powershell script to delete all temp files in a user profile. Also useful for terminal servers.

This script can be ran on any version of Windows, including Windows 10 and Server 2019. It’s not limited to just terminal servers.


Why delete files in the Downloads folder?

There are a number of reasons why you might want to do this. Downloads folders are often used as temporary folders. Junk files and programs get stored there, and 99% of the time your users never delete the files themselves.

In a terminal server/RemoteApp environment, whenever a user logs in – either through Remote Desktop (RDP) or through the RemoteApp URL – Windows will create a new folder/user profile under the C:\drive. This will of course create a Downloads, Desktop, Pictures, etc folder. (C:\users\username1\Downloads)

C:\users\username1, C:\users\username2, etc.

Let’s say you have 50 users that can log into your terminal server. If each user downloads a 1gb file to their Downloads folder, that’s an extra 50gb of data that you are backing up each night. And since it’s junk data and doesn’t really need to be backed up, that’s just increasing the time it takes to run your backup jobs, and filling up available space on the server.

So, I use a simple 1-line Powershell script that deletes the contents of the Download folder and have scheduled it to run Daily at 5pm using Windows Task Scheduler.


Powershell Script to Clean Up Downloads Folder

Before I show you the script to delete downloads from everyone, you’ll want to test it out with just 1 terminal server user. If it works as intended, you can use the 2nd script to apply it to all users.

How To Delete Contents of Downloads For 1 User

This script will empty Downloads for just 1 user. You should test this one first to make sure it works for you.

  1. Open Powershell.
  2. Paste in the code below. Replace the “Administrator” user with the username of any user in your C:\users\* path.
Get-ChildItem C:\Users\Administrator\Downloads\* | Remove-Item -recurse -force

The -recurse parameter is optional, but should be used so it will delete the content inside subdirectories as well.

How to Delete the Contents of a Downloads Folder for All Users

Not much changes with this script, other than we are using a wildcard (*) instead of hardcoding a specific user.

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

How To Delete Downloads Folder Contents every X Days

At this step, you have a few ways you can automate running this.

You can either save the Powershell script to the Windows Startup folder (C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp) so it runs whenever the computer reboots.

You can also use this for a logon script.

I prefer using Task Scheduler. It allows me to quickly modify WHEN the script runs and how frequently.


Schedule Download Content Deletion using Task Scheduler

  1. Open Task Scheduler from Start.
  2. Create Task.

General Tab:

Run Whether the User is Logged In or Not, check “Do not Store password”, check Run with Highest Privileges, and configure for Server 2019.

Triggers Tab:

Choose the date and schedule a time for the command to run.

Actions Tab:

Click New. Under Action, choose Start A Program.

Since this is such a short script, you can actually run it directly. You don’t need to point to a .ps1 file (although you can if you want to – see the last section of this tutorial).

Program/Script: Powershell
Add Arguments: Get-ChildItem C:\Users\*\Downloads\* | Remove-Item -recurse -force
Conditions/Settings/History can all be left default. Click OK once the task is created.


Test it Out

Now, simply right-click the new task > Run. It should successfully delete the contents of all users’ Downloads folder! To verify, browse to C:\users\username and click into a few users.

If everything went well, then you are good to go!


Powershell Script to Delete Temp Files

If you are also looking for a way to delete the Temp files for all users on a terminal server, you can use this script. The %TEMP% directory is located at C:\users\username\appdata\Local\Temp.

This script will loop through all users in the C:\users\ directory and recursively remove all the TEMP files.

If a user is currently logged into a terminal server session, there are probably files/programs in the %TEMP% directory that are in use and can’t be delete. Because of this, you may see errors. So, we are ignoring those in the script.

I recommend creating a GPO to auto log off users after X minutes of inactivity, and then setting this script to run when no one should be logged in (i.e – 4am).

Get-ChildItem -Path 'C:\Users' | foreach {
    Get-ChildItem -Path "$($_.FullName)\AppData\Local\Temp" -ErrorAction Ignore | Remove-Item -recurse -force
}

Alternate Scheduled Task Method

Alternatively, if you want to call the Powershell script instead of adding the script directly to the task, you can do that too. You might want to do this if you have a folder of scripts on a server already and want to keep things clean/consistent. For this example, I have a C:\Scripts folder on the server and have dropped my DeleteDownloadsDaily.ps1 into it.

Action: Start a Program
Program/Script: C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe
Add Arguments: <Path to your file> I.E – C:\Scripts\DeleteDownloadsDaily.ps1

Similar Posts

3 Comments

  1. Thanks a lot for this script ☺️

    1. You’re welcome! I’m glad it helped you out.

  2. Love this- thanks a lot. Just curious, how would you edit the script to apply only to the currently logged on user? My concern is that we have some administrators who log in and I don’t want them triggering this to delete the downloads from all users on that server.

Leave a Reply

Your email address will not be published. Required fields are marked *