-
Notifications
You must be signed in to change notification settings - Fork 2
/
Optimize-UserProfiles.ps1
137 lines (121 loc) · 5.25 KB
/
Optimize-UserProfiles.ps1
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<#
.SYNOPSIS
Removes common temporary files and folders older than $Days days old from user profiles.
Folders:
- USER\AppData\Local\Microsoft\Windows\WER'
- USER\AppData\Local\Microsoft\Windows\INetCache'
- USER\AppData\Local\Microsoft\Internet Explorer\Recovery'
- USER\AppData\Local\Microsoft\Terminal Server Client\Cache'
- USER\AppData\Local\CrashDumps'
- USER\AppData\Local\Temp
OPTIONAL: Remove specific filetypes from users' downloads folder.
.NOTES
Author: Tom de Leeuw
Website: https://ucsystems.nl / https://tech-tom.com
#>
function Optimize-UserProfiles {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[int] $Days,
[switch] $TempFiles,
[Parameter(Mandatory = $false, ParameterSetName = 'Downloads')]
[switch] $Downloads,
[Parameter(Mandatory = $false, ParameterSetName = 'Downloads')]
[switch] $ArchiveFiles,
[Parameter(Mandatory = $false, ParameterSetName = 'Downloads')]
[array] $ArchiveTypes = @('zip', 'rar', '7z', 'iso'),
[Parameter(Mandatory = $false, ParameterSetName = 'Downloads')]
[string] $ArchiveSize = '500MB',
[Parameter(Mandatory = $false, ParameterSetName = 'Downloads')]
[switch] $GenericFiles,
[Parameter(Mandatory = $false, ParameterSetName = 'Downloads')]
[array] $GenericTypes = @('msi', 'exe'),
[Parameter(Mandatory = $false, ParameterSetName = 'Downloads')]
[string] $GenericSize = '15MB'
)
begin {
# Get disk space for comparison afterwards
$Before = Get-DiskSpace
# Get all user folders, exclude administrators and default users
$Users = Get-UserFolders
# Parameters for Get-ChildItem and Remove-Item
$CommonParams = @{
Recurse = $true
Force = $true
Verbose = $true
ErrorAction = 'SilentlyContinue'
WarningAction = 'SilentlyContinue'
}
}
process {
ForEach ($Username In $Users) {
# General temp files/folders
if ($TempFiles -eq $true) {
# Folders to clean up
$TempFolders = @(
'\AppData\Local\Microsoft\Windows\WER',
'\AppData\Local\Microsoft\Internet Explorer\Recovery',
'\AppData\Local\Microsoft\Terminal Server Client\Cache',
'\AppData\Local\CrashDumps',
'\AppData\Local\Temp'
)
ForEach ($Folder In $TempFolders) {
If (Test-Path -Path "$env:SYSTEMDRIVE\Users\$Username\$Folder") {
try {
Get-ChildItem -Path "$env:SYSTEMDRIVE\Users\$Username\$Folder" @CommonParams |
Where-Object { ($_.CreationTime -and $_.LastAccessTime -lt $(Get-Date).AddDays(-$Days)) } |
Remove-Item @CommonParams
}
catch {
Write-Error $_
}
}
}
}
# Downloads folder
if ($Downloads -eq $true) {
If (Test-Path -Path "$env:SYSTEMDRIVE\Users\$Username\Downloads") {
# Compressed files larger than $ArchiveSize
if ($ArchiveFiles -eq $true) {
ForEach ($Ext In $ArchiveTypes) {
try {
Get-ChildItem -Path "$env:SYSTEMDRIVE\Users\$Username\Downloads\*.$Ext" @CommonParams |
Where-Object { ($_.CreationTime -and $_.LastAccessTime -lt $(Get-Date).AddDays(-$Days) -and $_.Length -gt $ArchiveSize) } |
Remove-Item @CommonParams
}
catch {
Write-Error $_
}
}
}
# Generic files larger than $GenericSize
if ($GenericFiles -eq $true) {
ForEach ($Ext In $GenericTypes) {
try {
Get-ChildItem -Path "$env:SYSTEMDRIVE\Users\$Username\Downloads\*.$($Ext)" @CommonParams |
Where-Object { ($_.CreationTime -and $_.LastAccessTime -lt $(Get-Date).AddDays(-$Days) -and $_.Length -gt $GenericSize) } |
Remove-Item @CommonParams
}
catch {
Write-Error $_
}
}
}
}
}
}
}
end {
# Get disk space again and calculate difference
$After = Get-DiskSpace
$TotalCleaned = "$(($After.FreeSpace - $Before.FreeSpace).ToString('00.00')) GB"
# Report
if ($null -ne $script:CleanupReport) {
$script:CleanupReport.UserProfiles = $TotalCleaned
}
else {
Write-Output "Total space cleaned: $TotalCleaned"
}
}
}