Remote uninstall software via powershell

23,291

Within PowerShell, this is very easy to do.

The below block of script will take a computer name, your username and password, connect to the remote computer and list all installed software by name:

$computerName = "SomeComputerName"
$yourAccount = Get-Credential
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
    Get-WmiObject Win32_Product | Select Name
}

When you have the name of the product you want to uninstall remotely - you can the perform an uninstall like this:

$computerName = "SomeComputerName"
$appName = "AppName"
$yourAccount = Get-Credential
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
    Get-WmiObject Win32_product | Where {$_.name -eq $appName} | ForEach {
        $_.Uninstall()
    }
}

In the above eaxmples - replace "SomeComputerName" with the name of the computer you wish to uninstall from.

You can also make the script prompt you for a computer name if you prefer with the following line:

$computerName = Read-Host "Enter Computer Name"

If you have multiple computers with the same piece of software that you want to uninstall - you can also define an array of computers to work with and do uninstalls from lots of machines:

$computerNames = @("SomeComputerName1", "SomeComputerName2", "SomeComputerName3")
$appName = "AppName"
$yourAccount = Get-Credential
ForEach ($computerName in $computerNames) {
    Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
        Get-WmiObject Win32_product | Where {$_.name -eq $appName} | ForEach {
            $_.Uninstall()
        }
    }
}
Share:
23,291

Related videos on Youtube

Cactusjack713
Author by

Cactusjack713

Updated on September 18, 2022

Comments

  • Cactusjack713
    Cactusjack713 over 1 year

    I have a user who works from home, but he's connected to our internal network. I have a remote access program (LANDESK) that I can connect to his system, but per our office policy, I can't remove programs unless I'm logged into my special admin account. Switching accounts disconnects this user from the VPN, effectively kicking me off his system.

    So as a fairly n00b PowerShell user, I'm attempting to come up with a means to run a command to execute the uninstaller for the software as my admin account while he's still logged in. Preferably silently. Windows 10 v1803. Software is SSMS 2014 if that helps.

    Thanks for suggestions.

    • Appleoddity
      Appleoddity over 4 years
      You can find the uninstall string in the registry and then execute it remotely using psexec or powershell remoting if you prefer.
  • Cactusjack713
    Cactusjack713 over 4 years
    For clarification, the quotes for things like "SomeComputerName" are meant to be filled in with actual info, correct?
  • Fazer87
    Fazer87 over 4 years
    absolutely... unless you happen to have a computer called SomeComputerName ;) I'll update my answer
  • Cactusjack713
    Cactusjack713 over 4 years
    Running a test on my neighbor's system gets an error: Invalid class "Win32_Procuct" + CategoryInfo : InvalidType: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetW‌​miObjectCommand
  • Cactusjack713
    Cactusjack713 over 4 years
    I think I see it. I think Product was misspelled
  • Fazer87
    Fazer87 over 4 years
    sorry about that - I wrote it in a rush... I started with the complete solution and then worked backwards to the simple audit and made a typo when stripping it back and re-keying bits. I've edited the answer to stop others having this issue