Public/Clear-FontCache.ps1

<#
    .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 {
        # Verify if running as Administrator
        Assert-RunAsAdministrator

        # Start timer
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew()

        # 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
            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 {
                Write-Verbose "Cleaning directory: $($Folder)"
                Get-ChildItem -Path $Folder -Filter $Filter @CommonParams | Remove-Item @CommonParams
                Write-Information "Finished removing font cache files."
            }
            catch {
                Write-Warning $_
            }
        }

        try {
            Write-Verbose 'Starting Font Cache service...'
            Get-Service -Name 'fontcache' | Start-Service
        }
        catch {
            Write-Warning $_
        }
    }

    end {
        New-CleanupReport -Name "FontCache"
    }
}