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
Set-ExecutionPolicy unrestricted -force -ErrorAction SilentlyContinue
$Folders = Get-ChildItem -Path "C:\Users"
if (Test-Path "${env:ProgramFiles(x86)}\Mozilla Firefox\uninstall\helper.exe"){
Start-Process -FilePath "${env:ProgramFiles(x86)}\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -ErrorAction SilentlyContinue
}elseif (Test-Path "${env:ProgramFiles}\Mozilla Firefox\uninstall\helper.exe"){
Start-Process -FilePath "${env:ProgramFiles}\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -ErrorAction SilentlyContinue
}
## Uninstalling for each user
ForEach ($Folder in $Folders){
$Userpath = "C:\Users\" $folder.name
if (Test-Path "$Userpath\AppData\Local\Mozilla Firefox\uninstall\helper.exe"){
Start-Process -FilePath "$Userpath\AppData\Local\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -ErrorAction SilentlyContinue
}
Start-Sleep 5
##Remove shortcuts from appdata
Remove-Item -Path "$userpath\AppData\Local\Mozilla" -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item -Path "$userpath\desktop\firefox.lnk" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$userpath\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" -Force -ErrorAction SilentlyContinue
}
if (Test-Path HKLM:\Software\Mozilla){
Remove-Item -Path HKLM:\SOFTWARE\Mozilla -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path HKLM:\SOFTWARE\mozilla.org){
Remove-Item -Path HKLM:\SOFTWARE\mozilla.org -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path HKLM:\SOFTWARE\MozillaPlugins){
Remove-Item -Path HKLM:\SOFTWARE\MozillaPlugins -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path HKLM:\SOFTWARE\WOW6432Node\Mozilla){
Remove-Item -Path HKLM:\SOFTWARE\WOW6432Node\Mozilla -Force -Recurse -ErrorAction SilentlyContinue
}elseif (HKLM:\SOFTWARE\WOW6432Node\mozilla.org){
Remove-Item -Path HKLM:\SOFTWARE\WOW6432Node\mozilla.org -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path HKLM:\SOFTWARE\WOW6432Node\MozillaPlugins){
Remove-Item -Path HKLM:\SOFTWARE\WOW6432Node\MozillaPlugins -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Firefox.lnk"){
Remove-Item -Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" -Force -ErrorAction
}
The only part of the script that I cannot get to work on is the removal of the registry keys. I need to run the script through Intune so I was told "The status of the script is based on the last return code of the last command. So if HKLM\Software\WOW6432Node\MozillaPlugins doesn’t exist, and it tries to delete it, it will return error code 1, not found. It would be best to use test-path with each remove-item so that the return code of every line is 0 unless it failed to delete in which case the failure status of the script is legitimate."
Simply put want to test the path of each registry key if exist then remove it if not move to the next and so on.
If I run (Test-Path HKLM:\Software\Mozilla) it came back true but running the script as a whole it will not remove the registry keys.
CodePudding user response:
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
}
}
}