Script To Clear Out Downloads Folder From Terminal Server UPDATED

Script To Clear Out Downloads Folder From Terminal Server

READING TIME: vii MINUTES

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 tin be scary, and so I'll prove you how to edit the script to run it on one user first to ensure information technology works correctly before deploying it to all terminal server users.

Once you verify the C:\Users\username\Downloads folder is empty, you tin use Task Scheduler to automate the process and set it to run daily, nightly, weekly, or at whatsoever 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 whatsoever version of Windows, including Windows 10 and Server 2019. It's non limited to only last servers.


Why delete files in the Downloads folder?

In that location are a number of reasons why y'all might want to do this. Downloads folders are often used as temporary folders. Junk files and programs go stored there, and 99% of the fourth dimension 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 contour under the C:\drive. This will of class create a Downloads, Desktop, Pictures, etc folder. (C:\users\username1\Downloads)

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

Let's say you accept 50 users that can log into your final 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'due south junk data and doesn't really demand to be backed upward, that'due south just increasing the time it takes to run your backup jobs, and filling up bachelor space on the server.

Then, I employ a uncomplicated 1-line Powershell script that deletes the contents of the Download folder and have scheduled it to run Daily at 5pm using Windows Chore Scheduler.


Powershell Script to Clean Up Downloads Folder

Before I testify yous the script to delete downloads from anybody, you lot'll want to test information technology out with just 1 terminal server user. If information technology works as intended, y'all can utilise the second script to use information technology to all users.

How To Delete Contents of Downloads For 1 User

This script will empty Downloads for just 1 user. Y'all should test this one beginning to make certain information technology 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-Detail -recurse -force        

The -recurse parameter is optional, simply should exist used so information technology volition delete the content within subdirectories as well.

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

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

          Go-ChildItem C:\Users\*\Downloads\* | Remove-Item -recurse -strength        

How To Delete Downloads Binder Contents every Ten Days

At this step, you have a few means 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 information technology 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 Job Scheduler

  1. Open Chore Scheduler from Starting time.
  2. Create Task.

Full 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 control to run.

Actions Tab:

Click New. Under Activity, choose Start A Program.

Since this is such a brusque script, you can actually run it directly. You lot don't need to point to a .ps1 file (although you can if you desire to – encounter the last department of this tutorial).

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


Test it Out

Now, merely right-click the new task > Run. Information technology 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 lot 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 last server, you tin utilise 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 utilise and can't exist delete. Because of this, yous may see errors. So, we are ignoring those in the script.

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

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

Alternating Scheduled Task Method

Alternatively, if you want to telephone call the Powershell script instead of adding the script directly to the job, yous can practice that too. Y'all might want to practice this if you lot have a folder of scripts on a server already and want to keep things clean/consistent. For this case, I take a C:\Scripts folder on the server and have dropped my DeleteDownloadsDaily.ps1 into information technology.

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

DOWNLOAD HERE

Posted by: croxtonappirdsmanne.blogspot.com

0 Komentar

Post a Comment




banner