PS 从多个位置卸载 Firefox 的脚本

PS Script to uninstall Firefox from multiple locations

我正在创建一个脚本来从多个位置卸载 Firefox。我有一个我创建的脚本,它在一定程度上有效。我已根据以下答案对我的原始脚本进行了更改以及一些其他更改

$LocalUsers = (Get-ChildItem -Path "C:\Users").name

# Uninstalling from Program Files
if (Test-Path "${env:ProgramFiles(x86)}\Mozilla Firefox\uninstall\helper.exe"){
    Start-Process -FilePath "${env:ProgramFiles(x86)}\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -Verbose #-ErrorAction SilentlyContinue
}
if (Test-Path "${env:ProgramFiles}\Mozilla Firefox\uninstall\helper.exe"){
    Start-Process -FilePath "${env:ProgramFiles}\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -Verbose #-ErrorAction SilentlyContinue
}

# Uninstalling for each user
ForEach ($LocalUser in $LocalUsers){
    $Userpath = "C:\Users\" + $LocalUser
    if (Test-Path "$Userpath\AppData\Local\Mozilla Firefox\uninstall\helper.exe"){
        Start-Process -FilePath "$Userpath\AppData\Local\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -Verbose #-ErrorAction SilentlyContinue
    }

    Start-Sleep 20

    # Remove shortcuts from appdata
    Remove-Item -Path "$userpath\AppData\Local\Mozilla" -Force -Recurse -Verbose #-ErrorAction SilentlyContinue
    Remove-Item -Path "$userpath\AppData\LocalLow\Mozilla" -Force -Recurse -Verbose #-ErrorAction SilentlyContinue
    Remove-Item -Path "$userpath\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" -Force -Verbose #-ErrorAction SilentlyContinue
    Remove-Item -Path "$userpath\desktop\firefox.lnk" -Force -Verbose #-ErrorAction SilentlyContinue
}

# Remove related registry keys
$pathToRemove = @(
    'HKLM:\Software\Mozilla'
    'HKLM:\SOFTWARE\mozilla.org'
    'HKLM:\SOFTWARE\MozillaPlugins'
    'HKLM:\SOFTWARE\WOW6432Node\Mozilla'
    'HKLM:\SOFTWARE\WOW6432Node\mozilla.org'
    'HKLM:\SOFTWARE\WOW6432Node\MozillaPlugins'
    'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Firefox.lnk'
)

foreach($path in $pathToRemove) {
    if(Test-Path $path) {
        try {
            Remove-Item $path -Recurse -Force -Verbose #-ErrorAction SilentlyContinue
        }
        catch {
            Write-Warning $_.Exception.Message
        }
    }
}

该脚本在卸载应用程序的某些机器上有效,但是,对于其他机器,它的踪迹会留在 Windows 程序文件中。它看起来像死了 link。我知道它已经死了 link 因为它缺少 Firefox 徽标。奇怪的是它指向 %localappdata%\Mozilla Firefox\uninstall\helper.exe 每个错误

应用程序安装后应该是什么样子(忽略版本只是网上的截图):

我假设问题是你的链式 if \ elseif \ else 条件,可能发生的情况是,如果第一个条件是 $true 你'仅删除第一个注册表项,然后退出链接条件(这是设计使然):

# only results in 'hello if' and then exits the chained conditions

if($true) {
    'hello if'
}
elseif($true) {
    'hello elseif'
}

在这种情况下,您可以做的是将所有路径存储在一个数组中,然后遍历它们,测试路径是否存在,如果存在,则将其删除:

$pathToRemove = @(
    'HKLM:\Software\Mozilla'
    'HKLM:\SOFTWARE\mozilla.org'
    'HKLM:\SOFTWARE\MozillaPlugins'
    'HKLM:\SOFTWARE\WOW6432Node\Mozilla'
    'HKLM:\SOFTWARE\WOW6432Node\mozilla.org'
    'HKLM:\SOFTWARE\WOW6432Node\MozillaPlugins'
    'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Firefox.lnk'
)

foreach($path in $pathToRemove) {
    if(Test-Path $path) {
        try {
            Write-Verbose "Attempting to remove: $path" -Verbose
            Remove-Item $path -Recurse -Force
            Write-Verbose "Successfully removed: $path" -Verbose
        }
        catch {
            Write-Warning $_.Exception.Message
        }
    }
}