Prioritize using oldprofile.ps1 as the source for the backup profile

This commit is contained in:
Cryostrixx 2024-10-01 07:22:47 -07:00
parent cee2919894
commit 13f2c77739
No known key found for this signature in database
GPG Key ID: 2FC11420AFB82801
2 changed files with 14 additions and 13 deletions

View File

@ -16,9 +16,17 @@ function Invoke-WinUtilInstallPSProfile {
} }
if ($NewHash.Hash -ne $OldHash.Hash) { if ($NewHash.Hash -ne $OldHash.Hash) {
if ((Test-Path $PSProfile) -and (-not (Test-Path "$PSProfile.bak"))) { if (Test-Path "$env:USERPROFILE\oldprofile.ps1") {
Write-Host "===> Backing Up Profile... <===" -ForegroundColor Yellow Write-Host "===> Backup File Exists... <===" -ForegroundColor Yellow
Copy-Item -Path $PSProfile -Destination "$PSProfile.bak" Write-Host "===> Moving Backup File... <===" -ForegroundColor Yellow
Copy-Item "$env:USERPROFILE\oldprofile.ps1" "$PSProfile.bak"
Write-Host "===> Profile Backup: Done. <===" -ForegroundColor Yellow
} else {
if ((Test-Path $PSProfile) -and (-not (Test-Path "$PSProfile.bak"))) {
Write-Host "===> Backing Up Profile... <===" -ForegroundColor Yellow
Copy-Item -Path $PSProfile -Destination "$PSProfile.bak"
Write-Host "===> Profile Backup: Done. <===" -ForegroundColor Yellow
}
} }
Write-Host "===> Installing Profile... <===" -ForegroundColor Yellow Write-Host "===> Installing Profile... <===" -ForegroundColor Yellow

View File

@ -5,7 +5,6 @@ function Invoke-WinUtilUninstallPSProfile {
#> #>
Invoke-WPFRunspace -ArgumentList $PROFILE -DebugPreference $DebugPreference -ScriptBlock { Invoke-WPFRunspace -ArgumentList $PROFILE -DebugPreference $DebugPreference -ScriptBlock {
# PSProfile parameter
param ($PSProfile) param ($PSProfile)
# Function to uninstall Nerd Fonts # Function to uninstall Nerd Fonts
@ -33,10 +32,7 @@ function Invoke-WinUtilUninstallPSProfile {
if ((Get-FileHash $PSProfile).Hash -eq $PSProfileHash) { if ((Get-FileHash $PSProfile).Hash -eq $PSProfileHash) {
# Uninstall OhMyPosh # Uninstall OhMyPosh
try { try {
# Get backup profile's content
$PSProfileContent = Get-Content "$PSProfile.bak" $PSProfileContent = Get-Content "$PSProfile.bak"
# Check if OhMyPosh is in use
$OhMyPoshInUse = $PSProfileContent -match "oh-my-posh init" $OhMyPoshInUse = $PSProfileContent -match "oh-my-posh init"
if (-not $OhMyPoshInUse) { if (-not $OhMyPoshInUse) {
if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) { if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) {
@ -61,10 +57,7 @@ function Invoke-WinUtilUninstallPSProfile {
# Uninstall Terminal-Icons # Uninstall Terminal-Icons
try { try {
# Get backup profile's content
$PSProfileContent = Get-Content "$PSProfile.bak" $PSProfileContent = Get-Content "$PSProfile.bak"
# Check if Terminal-Icons is in use
$TerminalIconsInUse = $PSProfileContent -match "Import-Module" -and $PSProfileContent -match "Terminal-Icons" $TerminalIconsInUse = $PSProfileContent -match "Import-Module" -and $PSProfileContent -match "Terminal-Icons"
if (-not $TerminalIconsInUse) { if (-not $TerminalIconsInUse) {
if (Get-Module -ListAvailable Terminal-Icons) { if (Get-Module -ListAvailable Terminal-Icons) {
@ -80,10 +73,7 @@ function Invoke-WinUtilUninstallPSProfile {
# Uninstall Zoxide # Uninstall Zoxide
try { try {
# Get backup profile's content
$PSProfileContent = Get-Content "$PSProfile.bak" $PSProfileContent = Get-Content "$PSProfile.bak"
# Check if Zoxide is in use
$ZoxideInUse = $PSProfileContent -match "zoxide init" $ZoxideInUse = $PSProfileContent -match "zoxide init"
if (-not $ZoxideInUse) { if (-not $ZoxideInUse) {
if (Get-Command zoxide -ErrorAction SilentlyContinue) { if (Get-Command zoxide -ErrorAction SilentlyContinue) {
@ -115,6 +105,9 @@ function Invoke-WinUtilUninstallPSProfile {
Write-Error "Failed to restore profile backup. Error: $_" -ForegroundColor Red Write-Error "Failed to restore profile backup. Error: $_" -ForegroundColor Red
} }
# Silently cleanup oldprofile.ps1 script
Remove-Item "$env:USERPROFILE\oldprofile.ps1" | Out-Null
# Silently cleanup $PSProfile.hash file # Silently cleanup $PSProfile.hash file
Remove-Item "$PSProfile.hash" | Out-Null Remove-Item "$PSProfile.hash" | Out-Null
} else { } else {