Using WMI uninstall commands with variables?

net1994 131 Reputation points
2021-04-12T23:58:32.183+00:00

Hello all – We have multiple versions of teamviewer software on multiple PCs. I need to get rid of them all via SCCM/MECM (about 1500 installs) and using WMI commands is a good bet it seems like. I don’t want to create long scripts with MSI Product code uninstall strings and what not. On our PCs. Installed TeamViewer is a mix of versions and products. In Add/Remove programs, it might be:

TeamViewer Host

TeamViewer 6

TeamViewer 13

TeamViewer

These were all manual installs. As there are multiple ways to remove each version, I’d like a ‘catch all’ using a common variable and WMI commands. So, I thought to use:

wmic product where "name like 'TeamViewer%%'" call uninstall

I ran the above with admin creds on a local pc with ‘TeamViewer 6’ installed. However, it says no instance found, even though it’s in Add/Remove programs. Can you see a syntax error with that command line?

Microsoft Configuration Manager
0 comments No comments
{count} votes

Accepted answer
  1. net1994 131 Reputation points
    2021-04-17T23:04:59.957+00:00

    After some excruciating trial and error, we found the below script that did the trick! Simple and elegant and quick. No need to install the NuGet module on systems. Copy and pasted this into our task sequence step in SCCM/MECM, and good to go! If you want to use for your own purposes, just replace 'TeamViewer Host" in the script with the name of the program you want to get rid of. This only works with MSI installed programs, not EXEs.

    $uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "TeamViewer Host" } | select UninstallString
    $uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "TeamViewer Host" } | select UninstallString
    
    if ($uninstall64) {
    $uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
    $uninstall64 = $uninstall64.Trim()
    Write "Uninstalling..."
    start-process "msiexec.exe" -arg "/X $uninstall64 /qn" -Wait}
    if ($uninstall32) {
    $uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
    $uninstall32 = $uninstall32.Trim()
    Write "Uninstalling..."
    start-process "msiexec.exe" -arg "/X $uninstall32 /qn" -Wait}
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. HanyunZhu-MSFT 1,846 Reputation points Microsoft External Staff
    2021-04-13T10:13:54.487+00:00

    @net1994

    Thanks for posting in Microsoft Q&A forum.

    TeamViewer cannot be found in the wmic product list when you run the command line wmic product get name, so you can't uninstall it with wmic command.

    You can refer to this article: https://www.manageengine.com/products/desktop-central/script-templates/software-remove/remove-teamviewer.html which have the same problem with you.
    Note: This is not from MS, just for your reference.

    Hope the above information is helpful to you.


    If the response is helpful, please click "Accept Answer"and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Jason Sandys 31,411 Reputation points Microsoft Employee Moderator
    2021-04-13T20:40:37.867+00:00

    You should never use Win32_Product, it has unintended side-effects that can/will cause issues. Serach the web for articles describing it as "Evil" for full details.

    See https://home.memftw.com/uninstall-software-en-masse/ for a possible path.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.