Search code examples
windowspowershelladmin

Uninstall Applications Via PowerShell


I have been working this for a couple days now, and no matter how I run this and work it, it seems to uninstall the program via PowerShell and returns the success code:

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0
PSComputerName   :

This happens with various notoriously difficult to remove software such as McAfee.

The command being used is:

 Get-WmiObject -Class win32_product -Filter "Name like '%McAfee%'" | ForEach-Object {$_.Uninstall()}

I've tried various scripts, solutions here, and variations of these (such as below).

$uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Yahoo Messenger" } | select UninstallString

$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Yahoo Messenger" } | select UninstallString

if ($uninstall64) {
    $uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
    $uninstall64 = $uninstall64.Trim()
    Write "Uninstalling (x64)..."
    start-process "msiexec.exe" -arg "/X $uninstall64 /qb" -Wait
    }
if ($uninstall32) {
    $uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
    $uninstall32 = $uninstall32.Trim()
    Write "Uninstalling (x32)..."
    start-process "msiexec.exe" -arg "/X $uninstall32 /qb" -Wait}

Even something simple like Yahoo Messenger, the command fails to uninstall the application when run from a Powershell Window as Administrator yet returns a success code and/or is no longer present on the WMI application list.


Solution

  • You can check the MSIInstaller events to find a clue why the uninstall failed:

    Get-WinEvent -computername <computername> -ProviderName MSIInstaller -Maxevents 30
    

    You can also log the MSI activity with /le '<logfilepath>' added to your invocation of msiexec.exe and check the results.

    I believe the msi install/uninstall operations are asynchronous. You may have to wait within your pssession until the install is finished.

    McAfee Agent sometimes requires frminst.exe /forceuninsall to be removed.