Forum Discussion
robmo
Jan 25, 2023Brass Contributor
Remove extra set of double quotes in string
Hi, I am writing a PowerShell script that needs to remove Firefox that is less than or equal to a specific version. I have the script working up to where it reads the uninstall string from the regis...
- Jan 30, 2023I did some inspection of Firefox installations and it turns out that they use the same uninstall string for every version. The only difference is if helper.exe is in Program Files (x86) or Program Files:
C:\Program Files (x86)\Mozilla Firefox\uninstall\helper.exe OR
C:\Program Files\Mozilla Firefox\uninstall\helper.exe
Since I have this information, I will just set some variables for these strings and then I won't have to deal with string handling when reading from the registry. This will simplify the development of this script.
Thank you for your help!
Jan 25, 2023
This should work : $uninstall= $uninstall -replace ('""'),('"')
robmo
Jan 25, 2023Brass Contributor
Harm_Veenstra,
What would be a good way to test if this solution works? I am using the following code but it fails at the initial assignment on Line 1 and throws an error:
$uninstall = ""C:\Program Files\Mozilla Firefox\uninstall\helper.exe""
$uninstall = $uninstall -replace('""'),('"')
$uninstall
Perhaps I need to use string handling at the time the value is retrieved from the registry?
- Jan 25, 2023Ah, ok.. You should use $uninstall='"C:\Program Files\Mozilla Firefox\uninstall\helper.exe"' to escape the double "
- Jan 26, 2023And how do you retrieve the value, perhaps we can fix that so that it's OK from the start
- robmoJan 26, 2023Brass Contributor
This is what I have so far. It fails at Line 43 when the double - double quotes are assigned to $uninstall. My debug attempt would be to correct the string from the registry before it is assigned using string manipulation. I will have some time to work on this more in a few hours. We are trying to uninstall Firefox versions less than or equal to 108.0.2 at this time.
[CmdletBinding()] param( [Parameter(Mandatory = $true)] [String]$RemoveVersions ) Function Test-IfRemovalNeeded() { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [String]$Version ) #Verify the version provided meets requirements #The version given and lower will be uninstalled if([version]$Version -le [version]$RemoveVersions) { #Version detected must be removed return 1 } else { #Version detected is compliant return 0 } } ########## ## Main ## ########## $displayName = "Mozilla Firefox" $regPaths = @("HKLM:SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*","HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*") #Searching for Firefox installations foreach($path in $regPaths) { #Collect all installed instances of Firefox $installed = Get-ItemProperty -Path $path | Where-Object {$_.DisplayName -match $displayName} if($null -ne $installed) { #Firefox was found #Test if the version(s) found should be removed foreach($item in $installed) { if(Test-IfRemovalNeeded -Version $item.DisplayVersion) { Write-Host "#Removal is needed" #Verify the uninstallstring path exists $uninstall = $item.UninstallString if(Test-Path $item.$uninstall) { #The uninstall string path was found $uninstallString = $uninstall + ' /s' Write-Host $uninstallString } } } } else { #Firefox is not installed QuitNow 0 } }
Thank you!
Rob