Skip to content

Commit

Permalink
Skip uninstalling apps that are in-use by the user's PowerShell profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryostrixx committed Oct 1, 2024
1 parent 193aea0 commit cee2919
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions functions/private/Invoke-WinUtilUninstallPSProfile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ function Invoke-WinUtilUninstallPSProfile {

$Fonts = Get-ChildItem $FontsPath -Recurse -Filter "*.ttf" | Where-Object { $_.Name -match $FontFamilyName }
if ($Fonts) {
Write-Host "===> Uninstalling Nerd Fonts... <===" -ForegroundColor Yellow
Write-Host "===> Uninstalling: Nerd Fonts... <===" -ForegroundColor Yellow
$Fonts | ForEach-Object {
if (Test-Path "$($_.FullName)") {
Remove-Item "$($_.FullName)"
}
}
} else {
Write-Host "===> Already Uninstalled Nerd Fonts. <===" -ForegroundColor Yellow
Write-Host "===> Already Uninstalled: Nerd Fonts. <===" -ForegroundColor Yellow
}
}

Expand All @@ -33,9 +33,18 @@ function Invoke-WinUtilUninstallPSProfile {
if ((Get-FileHash $PSProfile).Hash -eq $PSProfileHash) {
# Uninstall OhMyPosh
try {
if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) {
Write-Host "===> Uninstalling OhMyPosh... <===" -ForegroundColor Yellow
winget uninstall -e --id JanDeDobbeleer.OhMyPosh
# Get backup profile's content
$PSProfileContent = Get-Content "$PSProfile.bak"

# Check if OhMyPosh is in use
$OhMyPoshInUse = $PSProfileContent -match "oh-my-posh init"
if (-not $OhMyPoshInUse) {
if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) {
Write-Host "===> Uninstalling: OhMyPosh... <===" -ForegroundColor Yellow
winget uninstall -e --id JanDeDobbeleer.OhMyPosh
}
} else {
Write-Host "===> Skipped Uninstall: OhMyPosh In-Use. <===" -ForegroundColor Yellow
}
} catch {
Write-Error "Failed to uninstall OhMyPosh. Error: $_" -ForegroundColor Red
Expand All @@ -52,19 +61,37 @@ function Invoke-WinUtilUninstallPSProfile {

# Uninstall Terminal-Icons
try {
if (Get-Module -ListAvailable Terminal-Icons) {
Write-Host "===> Uninstalling Terminal-Icons... <===" -ForegroundColor Yellow
Uninstall-Module -Name Terminal-Icons
# Get backup profile's content
$PSProfileContent = Get-Content "$PSProfile.bak"

# Check if Terminal-Icons is in use
$TerminalIconsInUse = $PSProfileContent -match "Import-Module" -and $PSProfileContent -match "Terminal-Icons"
if (-not $TerminalIconsInUse) {
if (Get-Module -ListAvailable Terminal-Icons) {
Write-Host "===> Uninstalling: Terminal-Icons... <===" -ForegroundColor Yellow
Uninstall-Module -Name Terminal-Icons
}
} else {
Write-Host "===> Skipped Uninstall: Terminal-Icons In-Use. <===" -ForegroundColor Yellow
}
} catch {
Write-Error "Failed to uninstall Terminal-Icons. Error: $_" -ForegroundColor Red
}

# Uninstall Zoxide
try {
if (Get-Command zoxide -ErrorAction SilentlyContinue) {
Write-Host "===> Uninstalling Zoxide... <===" -ForegroundColor Yellow
winget uninstall -e --id ajeetdsouza.zoxide
# Get backup profile's content
$PSProfileContent = Get-Content "$PSProfile.bak"

# Check if Zoxide is in use
$ZoxideInUse = $PSProfileContent -match "zoxide init"
if (-not $ZoxideInUse) {
if (Get-Command zoxide -ErrorAction SilentlyContinue) {
Write-Host "===> Uninstalling: Zoxide... <===" -ForegroundColor Yellow
winget uninstall -e --id ajeetdsouza.zoxide
}
} else {
Write-Host "===> Skipped Uninstall: Zoxide In-Use. <===" -ForegroundColor Yellow
}
} catch {
Write-Error "Failed to uninstall Zoxide. Error: $_" -ForegroundColor Red
Expand Down

0 comments on commit cee2919

Please sign in to comment.