Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Windows Server 2019 / Clear Cache and Temp Files in User Profiles on Windows (RDS) with PowerShell and GPO

March 21, 2023 Group PoliciesPowerShellWindows 10Windows Server 2019

Clear Cache and Temp Files in User Profiles on Windows (RDS) with PowerShell and GPO

Windows Server RDS farm administrators are often faced with the problem of running out of space on a system drive due to a large amount of user data. This article shows you how to use PowerShell and Group Policies to automatically clean up the Recycle Bin, Downloads, Temp, and Cache folders in the user profiles on Windows.

Contents:
  • Automatically Cleanup Temp and Download Folders with Windows Storage Space
  • How to Empty the Recycle Bin for Windows Users?
  • Clear Cache, Temp, and Downloads Folders in the User Profile with PowerShell

Automatically Cleanup Temp and Download Folders with Windows Storage Space

You can use the built-in Storage Sense feature to automatically delete old and temporary files on Windows Server 2019/2022 and Windows 10/11. It has special GPO options that allow you to clean up Temp and Downloads folders.

Storage Space - delete temporary files

How to Empty the Recycle Bin for Windows Users?

By default, the Recycle Bin folder  ($Recycle.Bin) for deleted files is enabled on a Windows host. An individual Recycle Bin folder is created for each user in this directory (with the user’s SID as the name) on the RDS server. Over time, you will find that the total file size in the recycle bins of all users will take up a significant amount of disk space on the RDS host.

$Recycle.Bin filders of other user profiles

The default size of the Recycle Bin in Windows is about 5% of the disk size. You can change the maximum size of the Recycle Bin on each drive in its properties. Here you can also disable the Recycle Bin completely using the Don’t move files to the Recycle Bin option. However, this will only change the recycle bin settings for the current user.

configure recycle bin options

You may set the maximum Recycle Bin size for users with the Maximum allowed recycle bin size GPO option under User Configuration -> Administrative Templates -> Windows Components -> File Explorer. The maximum Recycle Bin size is set as a percentage of your disk size. Setting this to 0 disables the recycle bin on all drives.

GPO: Maximum recycle bin size allowed

To clear the Recycle bin in Windows, you can use the Clear-RecycleBin cmdlet (available in PowerShell 5.1 and newer on Windows 10). In order to empty the Recycle Bin without prompting, run the following command:

Clear-RecycleBin -Force

If you run this command as a common user on an RDS host, only the recycle bin of the current user will be cleared. You can add this command to your GPO logoff scripts to clear the recycle bin when a user signs out:

%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command Clear-RecycleBin -Confirm:$false

For previous versions of Windows (with older versions of PowerShell), you can use the following code to clean up the RecycleBin:

$Shell = New-Object -ComObject Shell.Application
$RecycleBin = $Shell.Namespace(0xA)
$RecycleBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}

Clear Cache, Temp, and Downloads Folders in the User Profile with PowerShell

Let’s consider a PowerShell script to clean up Temp, Downloads, and some other temporary folders in a user profile on a Windows Server RDS or a desktop computer running Windows 10/11.

Script comments:

  • In this example, we will delete files older than 14 days in the Downloads folder (you can change this option). Other folders with cache and temporary files will be completely empty;
  • The script runs in the current user context (the script deletes old files when a user logs out from Windows and runs as a GPO logoff script);
    If used in an RDS environment, ensure that users click the Logout/Login button to end their sessions. We also recommend configuring RDS session timeouts to end sessions after a period of inactivity automatically.
  • All information about deleted files will be saved to a text log file (you can disable this after debugging the script on test users);
  • It will also clean up the RDP connection history and cache;
  • The Windows Error Reporting (WER) folder in a user profile is cleaned;
  • The lines that clear the Google Chrome cache are commented out in the script. If your users use it and the Chrome cache takes up a lot of space, uncomment the lines with the paths;
  • You can add an extra operation to check the current size of the user profile folder before and after the cleanup (it allows you to get more accurate information, but takes some time). Or you can simply check the free disk space before and after (it is done quickly).
# You can use this script to clean folders in a user profile (cache, temp, downloads, google chrome cache) on RDS hosts, VDIs, or workstations
# Run the PowerShell script under a commaon user account (no administrator privileges are required). Only temporary files and the current user's cache are deleted.
# You can run this script via GPO (logoff script) or with the Task Scheduler
# Test the script in your environment and then remove the WhatIf option to permanently delete files
$Logfile = "$env:USERPROFILE\cleanup_profile_script.log"
$OldFilesData = (Get-Date).AddDays(-14)
# Complete cleanup of cache folders
[array] $clear_paths = (
    'AppData\Local\Temp',
    'AppData\Local\Microsoft\Terminal Server Client\Cache',
    'AppData\Local\Microsoft\Windows\WER',
    'AppData\Local\Microsoft\Windows\AppCache',
    'AppData\Local\CrashDumps'
    #'AppData\Local\Google\Chrome\User Data\Default\Cache',
    #'AppData\Local\Google\Chrome\User Data\Default\Cache2\entries',
    #'AppData\Local\Google\Chrome\User Data\Default\Cookies',
    #'AppData\Local\Google\Chrome\User Data\Default\Media Cache',
    #'AppData\Local\Google\Chrome\User Data\Default\Cookies-Journal'
)
# Folders where only old files should be removed
[array] $clear_old_paths = (
    'Downloads'
)
function WriteLog {
    Param ([string]$LogString)
    $Stamp = (Get-Date).ToString("yyyy/MM/dd HH:mm:ss")
    $LogMessage = "$Stamp $LogString"
    Add-content $LogFile -value $LogMessage
}
WriteLog "Starting profile cleanup script"
# If you want to clear the Google Chrome cache folder, stop the chrome.exe process
$currentuser = $env:UserDomain + "\"+ $env:UserName
WriteLog "Stopping Chrome.exe Process for $currentuser"
Get-Process -Name chrome -ErrorAction SilentlyContinue | Where-Object {$_.SI -eq (Get-Process -PID $PID).SessionId} | Stop-Process
Start-Sleep -Seconds 5
# Clean up cache folders
ForEach ($path In $clear_paths) {
    If ((Test-Path -Path "$env:USERPROFILE\$path") -eq $true) {
        WriteLog "Clearing $env:USERPROFILE\$path"
        Remove-Item -Path "$env:USERPROFILE\$path" -Recurse -Force -ErrorAction SilentlyContinue -WhatIf -Verbose 4>&1 | Add-Content $Logfile
    }
}
# Delete old files 
ForEach ($path_old In $clear_old_paths) {
    If ((Test-Path -Path "$env:USERPROFILE\$path_old") -eq $true) {
        WriteLog "Clearing $env:USERPROFILE\$path_old"
        Get-ChildItem -Path "$env:USERPROFILE\$path_old" -Recurse -Force -ErrorAction SilentlyContinue | Where-Object {($_.LastWriteTime -lt $OldFilesData)} | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue -WhatIf -Verbose 4>&1 | Add-Content $Logfile
    }
}
WriteLog "End profile cleanup script"

Similarly, you can add other folders you want to clear in a user profile to $clear_paths .

See my GitHub repo for the full version of the script: https://github.com/maxbakhub/winposh/blob/main/RDS/CleanupUserProfile.ps1.

This PowerShell script can run when the user’s RDP server session ends. The easiest way to assign a script is to use the logoff GPO policy.

  1. Create a new GPO and assign it to the Organization Unit (OU) in which your RDS hosts are located;
  2. Enable the option Configure user Group Policy Loopback Processing mode. This is necessary to apply the settings from the Users section to the computer; enable gpo loopback processing
    See Why Group Policy is not being applied to a computer/user for more information.
  3. Copy the PowerShell script file to the Net logon folder on your domain controller (\\woshub.com\netlogon\CleanupUserProfile.ps1);
    You can sign the PowerShell script code with a certificate to protect it from unauthorized modification.
  4. Go to the GPO section User Configuration -> Policies -> Windows Settings -> Scripts -> Logoff. Open the PowerShell Scripts tab and add a UNC path to the PS1 file in Netlogon;
  5. GPO: run powershell script at logoffLog off the user from Windows to apply the new GPO settings;
  6. When ending a user session on an RDS server, the specified folders will be cleared automatically. You can view a list of deleted files and folders in a text log file in a user profile.

profile cleanup log

You can also use the following methods that allow managing user profile size on Windows Server RDS servers:

  • How to remove old user profiles?
  • Enable NTFS disk quotas

The user folder cleanup methods discussed here can be applied to both locally stored user profiles and User Profile Disks or FSlogix profile containers on Windows Server RDSH.

6 comments
3
Facebook Twitter Google + Pinterest
previous post
Prevent Users from Creating New Groups in Microsoft 365 (Teams/Outlook)
next post
Send-MailMessage: Sending E-mails with PowerShell

Related Reading

Configure NTP Time Source for Active Directory Domain

May 6, 2025

View Windows Update History with PowerShell (CMD)

April 30, 2025

Change BIOS from Legacy to UEFI without Reinstalling...

April 21, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

6 comments

ntxadm May 28, 2023 - 6:17 pm

Thank you for the great script. Works like a charm 😉

Reply
BTRB November 9, 2023 - 5:23 pm

This is a great script; we plan to use it throughout our domain via GP. I do have one question, is there a way to append to the ‘cleanup_profile_script.log’?

Reply
BTRB November 9, 2023 - 5:53 pm

After running the script manually a couple times, I noticed that it does append so scratch the question above.
Would it be possible to add in the Clear-RecycleBin cmdlet to the script or do they need to be run separately?

Reply
admin November 20, 2023 - 7:56 am

Of course, you can add the Clear-RecycleBin command to the CleanupUserProfile.ps1 script.
It will only empty the recycle bin of the current user.

Reply
admin November 20, 2023 - 7:52 am

If you want to append information to the log file, you can simply send it via pipe:
"your info" | Add-Content $Logfile

Reply
Ferg November 6, 2024 - 7:13 pm

I notice it takes 2-3 mins of signing out.

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Updating List of Trusted Root Certificates in Windows
  • How to Delete Old User Profiles in Windows
  • Configure Google Chrome Settings with Group Policy
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Allow Non-admin Users RDP Access to Windows Server
  • Configuring FSLogix Profile Containers on Windows Server RDS
  • How to Backup and Copy Local Group Policy Settings to Another Computer
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top