Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

March 13, 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.

0 comment
0
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

How to Run Program without Admin Privileges and...

March 24, 2023

Configure Network Settings on Windows with PowerShell: IP...

March 24, 2023

Exchange Offline Address Book Not Updating in Outlook

March 21, 2023

Attaching Host USB Devices to WSL or Hyper-V...

March 20, 2023

Print Screen Key Not Working in Windows

March 17, 2023

Leave a Comment Cancel Reply

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

  • How to Run Program without Admin Privileges and Bypass UAC Prompt?

    March 24, 2023
  • Configure Network Settings on Windows with PowerShell: IP Address, DNS, Default Gateway, Static Routes

    March 24, 2023
  • Exchange Offline Address Book Not Updating in Outlook

    March 21, 2023
  • Attaching Host USB Devices to WSL or Hyper-V VM

    March 20, 2023
  • Sending an E-mail to a Microsoft Teams Channel

    March 17, 2023
  • How to Restore Deleted Users in Azure AD (Microsoft 365)?

    March 16, 2023
  • Fix: Remote Desktop Services Is Currently Busy

    March 15, 2023
  • Send-MailMessage: Sending E-mails with PowerShell

    March 14, 2023
  • Clear Cache and Temp Files in User Profiles on Windows (RDS) with PowerShell and GPO

    March 13, 2023
  • Prevent Users from Creating New Groups in Microsoft 365 (Teams/Outlook)

    March 6, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Updating List of Trusted Root Certificates in Windows
  • Configure Google Chrome Settings with Group Policy
  • How to Delete Old User Profiles in Windows?
  • How to Find the Source of Account Lockouts in Active Directory?
  • How to Disable or Enable USB Drives in Windows using Group Policy?
  • Changing Default File Associations in Windows 10 and 11
  • How to Automatically Fill the Computer Description in Active Directory?
Footer Logo

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


Back To Top