1

I am working on creating a script to uninstall Firefox from multiple locations. I have a script that I've created and it works to an extent. I have made changes to my original script based on the answer below plus some other changes

$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
        }
    }
}

The script has worked on some machines where it uninstalls the application, however, for others trace of it is being left behind in Windows Program Files. It is appearing as a dead link. I know it is a dead link because it is missing the Firefox logo. The strange thing is its points to %localappdata%\Mozilla Firefox\uninstall\helper.exe per the error

DealLink1 DeadLink2

Error 1 Error 2

What the app should look like if installed (ignoring the version just a screenshot from online):

ifinstalled

daaqis
  • 13
  • 1
  • 5
  • I'm assuming what's happening is that `if (Test-Path HKLM:\Software\Mozilla){` is `$true` hence it's only removing that registry key and then exiting the chained `elseif` conditions – Santiago Squarzon Mar 22 '22 at 22:30
  • Thanks I realized after the fact I was using elseif when I should have used if. – daaqis Mar 23 '22 at 14:35

3 Answers3

0

I'm assuming the problem is your chained if \ elseif \ else conditions, what could be happening is that if the first condition was $true you're only removing the first registry key and then exiting the chained conditions (this is by design):

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

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

What you can do in this case is store all the paths in an array and then loop over them, testing if the path exists and, if it does, remove it:

$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
        }
    }
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • So within my testing, it appears that traces f the app are being left behind. Windows Program and Feature still shows Mozilla Firefox. Although its missing the mozilla logo so perhaps just a dead link either way it shows, and then other reports detect it being installed still – daaqis Mar 25 '22 at 13:18
  • @daaqis did you check that those traces left behind are paths that exist in your `$pathToRemove` array? – Santiago Squarzon Mar 25 '22 at 13:22
  • Yes it is in the path: Windows cannot find 'C:\Users\hello1\AppData\Local\Mozilla Firefox\uninstall\helper.exe' An error occurred while trying to uninstall Mozilla Firefox (x64 en-CA) It may have already been uninstalled. Would you like to remove Mozilla Firefox 9x64 en-CA) from the Programs and Features list? I modified my original question with the newly updated script. – daaqis Mar 25 '22 at 22:10
  • @daaqis well that's completely different from your original question and not how SO works. If you have a new requirement you should ask a new question. My answer intends to answer your original question and will not be updated – Santiago Squarzon Mar 25 '22 at 22:57
  • thanks for the info I've asked a new question. – daaqis Mar 27 '22 at 19:39
0

I was having this same issue from a failed winget install, and found a registry key that was left behind. When it was removed from the list and allowed for reinstallation.

add to your registry key removal array: 'HKCU:\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\UNINSTALL\Mozilla Firefox*'

0

I think in order to have it "removed" for each user profile..
You would need to during the ForEach ($LocalUser in $LocalUsers) { section load the registry hive of the user to "User" then search and remove the reg key from the loaded hive.. Then unload the hive...

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Jay
  • 1
  • Another option is to load the user reg hive, and add a runonce that runs the silent uninstall string. then when the other users log in it gets uninstalled as them.. – Jay Aug 11 '23 at 18:22