Public/Optimize-UserProfiles.ps1

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

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

        # 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'
                )
                $TempFilesToClean = ForEach ($FolderName In $TempFolders) {
                    $FolderToClean = "$env:SYSTEMDRIVE\Users\$UserName\$FolderName"
                    If (Test-Path -Path $FolderToClean) {
                        try {
                            Get-ChildItem -Path $FolderToClean -Recurse -Force -ErrorAction 'SilentlyContinue' |
                                Where-Object { ($_.CreationTime -and $_.LastAccessTime -lt $(Get-Date).AddDays(-$Days)) }
                        }
                        catch {
                            Write-Error $_
                        }
                    }
                }
                $TempFilesToClean | ForEach-Object {
                    if ($_.PSIsContainer) {
                        Write-Information "Cleaning directory: $($_.FullName)"
                    }
                    Remove-Item $_.FullName -Recurse -Force -ErrorAction 'SilentlyContinue'
                }
                Write-Information "Finished removing generic temp files for $Username."
            }

            # 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 {
        New-CleanupReport -Name "UserProfiles"
    }
}