question

net1994-7989 avatar image
0 Votes"
net1994-7989 asked CourtneyRegan-5428 commented

Using WMI uninstall commands with variables?

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?

mem-cm-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

net1994-7989 avatar image
0 Votes"
net1994-7989 answered CourtneyRegan-5428 commented

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}
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So, it looks like you just converted my VBScript to PowerShell. Not sure why'd you do that, but if it works for you, great.

0 Votes 0 ·

Hi Jason

I found this snippet on spiceworks. I didn't know it was based on your work as it wasn't attributed. But I sincerely thank you! I did originally try your VBS, but it wasn't working for me. Either it was an issue with our environment or errors between the keyboard and chair. Thanks again for getting us over the hump!

0 Votes 0 ·
HanyunZhu-MSFT avatar image
1 Vote"
HanyunZhu-MSFT answered HanyunZhu-MSFT edited

@net1994-7989

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jason-MSFT avatar image
1 Vote"
Jason-MSFT answered Jason-MSFT commented

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.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Jason for the heads up.

My colleague and I worked on this all day today. For all older versions, there is a uninstall.exe program we can run and it removes it that way without WMI getting involved. We were able to get this to work via 'Run Command Line' in a SCCM/MECM Task Sequence. However for the current version of TeamViewer, there isn't a similar uninstall.exe utility. I'd rather not use VB script (per your link) to remove all the older versions (we already have that covered in the task sequence) and the newer one.

Powershell is also an option for the current version. When I used this:

  1. $application = Get-WmiObject -Class Win32_Product -Filter "Name = 'TeamViewer Host'"

  2. $application.Uninstall()

Powershell removed it right away without any MSI or EXE commands. So we might just roll with that for the current version and possibly the older ones (even though the uninstall.exe works fine).

0 Votes 0 ·

That's still using WIn32_Product which has the same terrible side effects regardless of how you call it.

All the VBScript is doing is dynamically looking up the Uninstall command-line from the ARP section of the registry. You can look this up manually as well and add the listed uninstall command-line to your TS.

0 Votes 0 ·
  • to this.

0 Votes 0 ·

Thanks again Jason. The one thing is that there could be dozens of different MSI product codes for this one version of TeamViewer (15.x) as other regions have installed MSIs at different times with different product codes built into each MSI by the vendor. So we would need a script to enumerate it on the PC and then uninstall. Could you point us to a script like this?

Or is there a different Powershell command that could do this WITHOUT invoking Win32_Product? The closest option I found would be:

Get-Package -Name TeamViewer* | Uninstall-Package However this one requires downloading the optional NuGet module, which we want to avoid. Thoughts.



0 Votes 0 ·
Show more comments