1

I'm trying to understand the .uninstall() method.

From this link it looks like the method .uninstall() only works when used with Get-WmiObject -Class Win32_Product. But this means it will consider 32-bit software only and not 64-bit software.

So I wrote this few lines in order to uninstall Erlang, which is 64-bit:

# Check if a Software ins installed
function Check_Program_Installed($programName) {
    $x86_check = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
                 Get-ItemProperty |
                 Where-Object {$_.DisplayName -like "*$programName*" } |
                 Select-Object -Property DisplayName, UninstallString) |
                 Format-Table

    if (Test-Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall') {
        $x64_check = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") | Get-ItemProperty | Where-Object {$_.DisplayName -like "*$programName*" } | Select-Object -Property DisplayName, UninstallString) | Format-Table
    }
    if ($x86_check -and $x64_check -eq $null) {
        Write-Host "$programName is not installed on this computer" -ForegroundColor Green
        #continue
    } elseif ($x86_check -or $x64_check -ne $null) {
        Write-Host "On this computer is installed " -ForegroundColor Red
        $x86_check
        $x64_check

        $x86_check.uninstall()
        $x64_check.uninstall()
    }
}

# Erlang check
Write-Host "Checking if Erlang exist    " -NoNewline
Check_Program_Installed("Erlang")
Write-Host "The End: the script ends here" -ForegroundColor Yellow

but unfortunately it returns me the error:

You cannot call a method on a null-valued expression. At C:\Users\Admin\Desktop\test.ps1:17 char:3 + $x86_check.uninstall() + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

Method invocation failed because [Microsoft.PowerShell.Commands.Internal. Format.FormatStartData] does not contain a method named 'Uninstall'. At C:\Users\Admin\Desktop\test.ps1:18 char:3 + $x64_check.uninstall() + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

I believe the root cause it that there are DisplayName and UninstallString in the variable, right?

A way out I found is to use:

'"C:\Program Files\erl8.3\Uninstall.exe'" | cmd

in order to uninstall but this is not using the .uninstall() method which is what I want to use.

Is Microsoft saying that you can use .uninstall() only with 32-bit architecture and for 64-bit you need to find your own way out?

If so it's quite rudimentary

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
  • 2
    Don't use `Format-*` cmdlets for further _programmatic_ processing - these cmdlets are intended solely to create output for _display_. – mklement0 Feb 17 '19 at 20:23
  • that error message means that the `x86` version is not installed. you need to add a test for _each one_ of those reg keys. your current test is only for "one or the other". – Lee_Dailey Feb 17 '19 at 20:29

1 Answers1

0

The reply is NO.

.uninstall() can only be used with Get-WmiObject -Class Win32_Product and therefore will only uninstall 32-bit programs.

There might be an alternative way out uninstall both 32-bit and 64-bit program with:

Get-Package "*Erlang*"

At least finds the program but

Get-Package "*Erlang*" | Uninstall-Package -Force

won't uninstall

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113