-
Notifications
You must be signed in to change notification settings - Fork 2
/
Clear-FontCache.ps1
70 lines (61 loc) · 1.88 KB
/
Clear-FontCache.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
<#
.SYNOPSIS
Clears user font cache files located in "C:\Windows\ServiceProfiles\LocalService\AppData\Local\"
.NOTES
Author: Tom de Leeuw
Website: https://ucsystems.nl / https://tech-tom.com
#>
function Clear-FontCache {
begin {
# Get disk space for comparison afterwards
$Before = Get-DiskSpace
# Location of FontCache files
$Folder = "C:\Windows\ServiceProfiles\LocalService\AppData\Local\"
# Grab only the user cache files
$Filter = "FontCache-S-*.dat"
# Parameters for Get-ChildItem and Remove-Item
$CommonParams = @{
Recurse = $true
Force = $true
Verbose = $true
ErrorAction = 'SilentlyContinue'
WarningAction = 'SilentlyContinue'
}
}
process {
try {
Write-Verbose 'Stopping Font Cache service...'
Get-Service -Name 'fontcache' | Stop-Service
}
catch {
Write-Warning $_
}
if (Test-Path -Path $Folder) {
try {
Get-ChildItem -Path $Folder -Filter $Filter @CommonParams | Remove-Item @CommonParams
}
catch {
Write-Warning $_
}
}
try {
Write-Verbose 'Starting Font Cache service...'
Get-Service -Name 'fontcache' | Start-Service
}
catch {
Write-Warning $_
}
}
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.FontCache = $TotalCleaned
}
else {
Write-Output "Total space cleaned: $TotalCleaned"
}
}
}