ClearCache.ps1

<#
.SYNOPSIS
Clears various caches on the system and empties the Recycle Bin.
 
.DESCRIPTION
Prompts the user for admin priveledges.
Creates a restore point before making any changes to the device.
Creats a log file and path, if one isnt present.
Identifies if the system is Windows, Mac or Linux then clears temporary files and caches based on system.
Empties recycle bin to free up disk space.
Self-destructs once the process has been complete.
 
#>


function ClearCache {
    [CmdletBinding()]
    param (
        [string[]] $windowsPaths = @(
            "$env:LOCALAPPDATA/Temp/*",
            "$env:SystemRoot/Temp/*",
            "$env:LOCALAPPDATA/Microsoft/Windows/Explorer/*",
            "$env:APPDATA/Microsoft/Windows/Recent/*",
            "$env:SystemRoot/prefetch/*",
            "$env:SystemRoot/SoftwareDistribution/*",
            "$env:LOCALAPPDATA/Microsoft/Windows/INetCache/*",
            "$env:APPDATA/Microsoft/Windows/Cookies/*",
            "$env:LOCALAPPDATA/Microsoft/Edge/User Data/Default/Cache/Cache_Data/*",
            "$env:LOCALAPPDATA/Microsoft/Edge/User Data/Default/Cache/Cookies/*",
            "$env:LOCALAPPDATA/Google/Chrome/User Data/Default/Cache/*",
            "$env:LOCALAPPDATA/Google/Chrome/User Data/Default/Cookies/*",
            "$env:LOCALAPPDATA/Mozilla/Firefox/Profiles/aieex4lw.default/cache2/*",
            "$env:LOCALAPPDATA/Mozilla/Firefox/Profiles/aieex4lw.default/cookies.sqlite/*",
            "$env:LOCALAPPDATA/Packages/Microsoft.WindowsStore_8wekyb3d8bbwe/LocalState/Cache/*",
            "$env:LOCALAPPDATA/BraveSoftware/Brave-Browser/User Data/Default/Cache/Cache_Data/*",
            "$env:APPDATA/Opera Software/Opera Stable/Cache/*",
            "$env:APPDATA/Opera Software/Opera Stable/Cookies/*"
        ),
        [string[]] $darwinPaths = @(
            "/tmp/*",
            "/var/folders/...",
            "~/Library/Application Support/Google/Chrome/Default/Cache/*",
            "~/Library/Application Support/Google/Chrome/Default/Cookies/*",
            "~/Library/Application Support/Firefox/Profiles/aieex4lw.default/cache2/*",
            "~/Library/Application Support/Firefox/Profiles/aieex4lw.default/cookies.sqlite",
            "~/Library/Application Support/BraveSoftware/Brave-Browser/Default/Cache/Cache_Data/*",
            "~/Library/Application Support/com.operasoftware.Opera/Cache/*",
            "~/Library/Application Support/com.operasoftware.Opera/Cookies/*"
        ),
        [string[]] $linuxPaths = @(
            "/tmp/*",
            "/var/tmp/*",
            "~/.cache/google-chrome/Default/Cache/*",
            "~/.config/google-chrome/Default/Cookies/*",
            "~/.mozilla/firefox/aieex4lw.default/cache2/*",
            "~/.mozilla/firefox/aieex4lw.default/cookies.sqlite",
            "~/.config/BraveSoftware/Brave-Browser/Default/Cache/Cache_Data/*",
            "~/.config/opera/Opera Stable/Cache/*",
            "~/.config/opera/Opera Stable/Cookies/*"
        ),
        [string] $logPath = "$env:USERPROFILE/Documents/Logs/CC.log"
    )

    $ErrorActionPreference = "SilentlyContinue"

    $logsDir = Split-Path -Path $logPath
    if (-not (Test-Path -Path $logsDir -PathType Container)) {
        New-Item -Path $logsDir -ItemType Directory -Force
    }

    $isWindows = $env:OS -like '*Windows_NT*'
    $isDarwin = $env:OS -like '*Darwin*'
    $isLinux = $env:OS -like '*Linux*'

    try {
        if ($isWindows) {
            Checkpoint-Computer -Description $date -RestorePointType MODIFY_SETTINGS
            foreach ($path in $windowsPaths) {
                Remove-Item -Path $path -Force -Recurse
            }
            Clear-RecycleBin -Force
        }
        elseif ($isDarwin) {
            foreach ($path in $darwinPaths) {
                Remove-Item -Path $path -Force -Recurse
            }
        }
        elseif ($isLinux) {
            foreach ($path in $linuxPaths) {
                Remove-Item -Path $path -Force -Recurse
            }
        }
        else {
            Write-Output "Unsupported operating system."
        }

        $successMessage = "Caches cleared successfully on $currentDate"
        Write-Output $successMessage
        if ($logPath) {
            $successMessage | Out-File -Append -FilePath $logPath
        }
    }
    catch {
        $errorMessage = "Error: $_"
        Write-Output $errorMessage -ForegroundColor Red
        if ($logPath) {
            $errorMessage | Out-File -Append -FilePath $logPath
        }
    }
}

<#
$scriptPath = $MyInvocation.MyCommand.Path
function selfDestruct {
    Start-Sleep -Seconds 5
    Remove-Item -Path $scriptPath -Force
}
selfDestruct
#>