Redirected Folders on RDS Session hosts tend to cause a specific issue:
Each user’s set of Redirected Folders creates their own Recycle Bin entry, which can grow quite a bit.
The following function can display a list of files contained within the Recycle Bins older than the provided age.
Even better, the script has a switch parameter Remove which does exactly what you think it does 🙂
Through a scheduled task you can run this script on a regular basis when required.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
Function Clean-RedirectedRecycleBin {
<#
.SYNOPSIS
Display and if required clean up recycle bin contents older than retention period for all redirected folders
.DESCRIPTION
Check all recycle bin files on a share for files older than the retention period.
If switch to remove is activated, forcefully delete the files within.
.PARAMETER SharePath
Location of the share which contains Recycle Bin folders
.PARAMETER RetentionPeriod
Retention period of files found within the recycle bin
.PARAMETER Remove
A switch parameter which wil forcefully remove files if activated
.NOTES
Name: Clean-RedicretedRecycleBin.ps1
author: Robert Prüst
DateCreated: 29-09-2015
.EXAMPLE
Clean-RedirectedRecycleBin -SharePath '\\server01\users\profiles' -RetentionPeriod 20 -Remove
Looks for RecycleBin items within \\server01\users\profiles which are older than 20 days and will remove said items.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
$SharePath = '\\SERVER\SHARE',
[Parameter(Mandatory=$True)]
[int]$RetentionPeriod = 60,
[Switch]$Remove
)
$PSD = 'RecycleDrive'
Write-Verbose -Message "Creating PSDrive for $SharePath"
$Null = New-PSDrive -Name $PSD -PSProvider FileSystem -Root $SharePath -Description 'Temporary Drive to assist cleaning up Recycle Bins in Redirected Folders'
Write-Verbose -Message "Scanning all folders in $SharePath for files older than $RetentionPeriod days"
$Folders = Get-ChildItem -Path "$($PSD):" -Force -Recurse -ErrorAction SilentlyContinue | Where-Object {$_.FullName -match 'Recycle.bin' -AND ((Get-Date).AddDays(-$RetentionPeriod) -gt $_.LastWriteTime) -AND ($_.PSIsContainer)}
foreach ($Folder in $Folders) {
$Files = Get-ChildItem -Path $Folder.FullName -Recurse -Force | Where-Object {-not $_.PSIsContainer}
foreach ($File in $Files){
$properties = @{'FileName' = $File.FullName;
'SizeMB' = ($File.Length /1MB -as [int]);
'LastAccessTime' = $File.LastAccessTime;
'LastWriteTime' = $File.LastWriteTime}
$obj = New-Object -TypeName PSObject -Property $properties
if($Remove) {
Write-Verbose -Message "Removing file $($obj.filename)"
$obj.filename | Remove-Item -Force -Recurse
}
else {
Write-Output $obj
}
}
}
Write-Verbose -Message "Removing PSDrive $PSD"
Remove-PSDrive -Name $PSD
}
|