public/Test-PSProfileHash.ps1

function Test-PSProfileHash {
    <#
    .SYNOPSIS
        Test the current user's profile hash against the expected stored hash
    .DESCRIPTION
        Test the current user's profile hash against the expected stored hash.
        This function is designed to be called at the beginning of your PowerShell profile
        to detect if the profile file has been modified since the last hash export.
    .INPUTS
        None
    .OUTPUTS
        System.Boolean
        Returns $true if the profile hash matches the stored hash, $false otherwise.
    .EXAMPLE
        Test-PSProfileHash

        Compares the current profile's hash against the most recently saved hash file.
    .NOTES
        None
    #>


    try {
        $profilesPath = Split-Path -Path $profile -Parent
        $latestHashFile = Get-ChildItem -Path $profilesPath -Filter "*.hash" -ErrorAction Stop | 
            Sort-Object -Property CreationTime | 
            Select-Object -Last 1
        
        if (-not $latestHashFile) {
            Write-Warning "⚠️ No profile hash file found. Run 'Export-PSProfileHash' first."
        }

        $lastProfileHash = Get-Content -Path $latestHashFile.FullName -ErrorAction Stop
    }
    catch {
        Write-Error "❌ Could not read stored profile hash."
        throw $_.Exception.Message
    }

    try {
        $currentProfileHash = Get-FileHash -Path $profile -Algorithm SHA256 -ErrorAction Stop | 
            Select-Object -ExpandProperty Hash
    }
    catch {
        Write-Error "❌ Could not calculate current profile hash."
        throw $_.Exception.Message
    }

    if ($lastProfileHash -eq $currentProfileHash) {
        Write-Host "✅ Profile hash matches." -ForegroundColor Green
    }
    else {
        try {
            Write-Host @"
██████╗ ███████╗██████╗ ██████╗ ██████╗ ███████╗██╗██╗ ███████╗██╗ ██╗ █████╗ ████████╗ ██████╗██╗ ██╗███████╗██████╗
██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔═══██╗██╔════╝██║██║ ██╔════╝██║ ██║██╔══██╗╚══██╔══╝██╔════╝██║ ██║██╔════╝██╔══██╗
██████╔╝███████╗██████╔╝██████╔╝██║ ██║█████╗ ██║██║ █████╗ ██║ █╗ ██║███████║ ██║ ██║ ███████║█████╗ ██████╔╝
██╔═══╝ ╚════██║██╔═══╝ ██╔══██╗██║ ██║██╔══╝ ██║██║ ██╔══╝ ██║███╗██║██╔══██║ ██║ ██║ ██╔══██║██╔══╝ ██╔══██╗
██║ ███████║██║ ██║ ██║╚██████╔╝██║ ██║███████╗███████╗╚███╔███╔╝██║ ██║ ██║ ╚██████╗██║ ██║███████╗██║ ██║
"@

            
            Write-Host "`nProfile hash does not match; verify these changes are expected and if so, run Export-PSProfileHash to update the stored hash." -ForegroundColor Yellow
            Write-Host "`nIf you did not expect these changes, abort loading the profile and remove unexpected changes prior to next load." -ForegroundColor Yellow
            $latestBackupFile = Get-ChildItem -Path $profilesPath -Filter "*.backup" -ErrorAction Stop | 
                Sort-Object -Property CreationTime | 
                Select-Object -Last 1
            
            if ($latestBackupFile) {
                $lastProfileContent = Get-Content -Path $latestBackupFile.FullName -ErrorAction Stop
                $currentProfileContent = Get-Content -Path $profile -ErrorAction Stop
                
                Write-Host "`n----- Profile Changes -----" -ForegroundColor Yellow
                Compare-Object -ReferenceObject $lastProfileContent -DifferenceObject $currentProfileContent | 
                    Out-String | 
                    Write-Host
            }

            $userInput = Read-Host "Do you want to continue loading the profile? (y/N)"
            switch ( $userInput ) {
                'y' {
                    continue
                }
                'Y' {
                    continue
                }
                default {
                    Write-Host "Aborting profile load." -ForegroundColor Red
                    exit
                }
            }
        }
        catch {
            Write-Warning "⚠️ Could not display profile differences."
        }
    }
}