public/Export-PSProfileHash.ps1
|
<# .SYNOPSIS Saves the current user's profile hash for comparison at next run. .DESCRIPTION Saves the current user's profile hash for comparison at next run. .INPUTS None .OUTPUTS None .EXAMPLE Export-PSProfileHash Writes the following files to the current user's profile directory (e.g.): - Microsoft.VSCode_profile.ps1_83580fb6-fc89-4756-8a58-b1f4d3f3b7ff.hash - Microsoft.VSCode_profile.ps1_83580fb6-fc89-4756-8a58-b1f4d3f3b7ff.backup The hash file contains the SHA256 hash of the current user's profile at that point. The backup file contains a copy of the current user's profile at that point. .NOTES None #> function Export-PSProfileHash { $guid = [guid]::NewGuid() $profileHashFile = "$($profile)_$($guid).hash" $profileBackupFile = "$($profile)_$($guid).backup" try { $Hash = Get-FileHash -Path $profile -Algorithm SHA256 -ErrorAction Stop | Select-Object -ExpandProperty Hash } catch { Write-Warning "⚠️ Could not calculate hash for profile." throw $_.Exception.Message } try { $Hash | Out-File -FilePath $profileHashFile -Encoding ASCII -Force -ErrorAction Stop Write-Host "✅ Profile hash saved successfully to '$profileHashFile'." -ForegroundColor Green } catch { Write-Error "❌ Could not write hash to file." throw $_.Exception.Message } try { Copy-Item -Path $profile -Destination $profileBackupFile -Force -ErrorAction Stop Write-Host "✅ Profile backup saved successfully to '$profileBackupFile'." -ForegroundColor Green } catch { Write-Error "❌ Could not save profile backup to '$profileBackupFile'." throw $_.Exception.Message } } |