-1

"wmic product call uninstall" EXCEPT for specified criteria?

Above is my original question.

I think, at this point, I need to use a FOR loop. Here's what I've managed to come up with:

FOR /F "delims=" %%G NOT IN "product-list.txt" DO wmic product get name

As stated in my original question, my ultimate goal is to be able to run call uninstall, but only for products that are not in product-list.txt.

I tried running the above as a test, and this happened:

FOR /F "delims=" %G "NOT IN 'product-list.txt'" DO wmic product get name
"NOT IN 'product-list.txt'" was unexpected at this time.

Is there any way to run this, or do I need to use something other than a FOR loop?

EDIT: @Compo in the comments raised a valid question of how I made product-list.txt. Command below.

wmic product get name | find /I /V "Name" > "%temp%\product-list.txt"

Thanks in advance for your help.

R3TURN-0
  • 83
  • 1
  • 9
  • The first thing we need specific information on is the exact command line you used to produce your base list, _(`product-list.txt`)_. – Compo Jul 21 '23 at 18:31
  • @Compo (```product-list.txt```) was created from the output of (```wmic product get name```) on a fresh machine. The idea is that I will be able to silently remove anything on the list that is NOT found on a freshly installed machine. – R3TURN-0 Jul 21 '23 at 18:37
  • I needed an answer to my comment @R3TURN-0, because just using `wmic product get name >"%temp%\product-list.txt"` or the correct way `wmic /output:"%temp%\product-list.txt" product get name` will create a nasty unicode, (UTF-16 LE), file containing both a title and some problematic characters. FindStr with `/G` will have issues trying to properly parse that file. – Compo Jul 21 '23 at 18:41
  • @Compo I apologize. code, which I mistyped above and will edit, below: ```wmic product get name | find /I /V "Name">"%temp%\product-list.txt"``` – R3TURN-0 Jul 21 '23 at 18:44
  • Well that command alone has immediately excluded all software which includes the case insensitive string `Name`. Not what I would call a robust method of listing all of the base software which used a Microsoft installer. – Compo Jul 21 '23 at 18:51
  • @Compo My goal was to exclude the ```name``` at the top of the usual output so it doesn't get counted. It seems to have worked, as I looked through and all programs are showing on the list in the file. – R3TURN-0 Jul 21 '23 at 18:57
  • That was just luck, you obviously didn't have software which used that string. However it is important to mention it because the target PC may have installed a 'product' named with the string `name` somewhere within it, and if you were to use the same listing method those would be omitted, and inevitably not be 'call'ed for uninstall. Personally I would have created the initial list from the Command Prompt like this: ```(For /F Tokens^=6^ Delims^=^"^ EOL^= %G In ('%SystemRoot%\System32\wbem\WMIC.exe Product Get Name /Format:MOF') Do @Echo %G) 1>"%TEMP%\product-list.txt"```. – Compo Jul 21 '23 at 19:04

1 Answers1

1

Important Note: When Win32_Product is queried, as here, it runs a consistency check on all Windows Installer applications, and performs automatic and silent repairs if needed. This may have undesired results on the target PC, and could make the already slow WMIC command even slower.


As an extension of my comments, in order to have a problem free base product list, I would advise that you create that list like this.

From a Command Prompt window:

(For /F Tokens^=6^ Delims^=^"^ EOL^= %G In ('%SystemRoot%\System32\wbem\WMIC.exe Product Get Name /Format:MOF') Do @Echo %G) 1>"%TEMP%\product-list.txt"

From a batch file:

@(  For /F Tokens^=6^ Delims^=^"^ EOL^= %%G In ('
     %SystemRoot%\System32\wbem\WMIC.exe Product Get Name /Format:MOF
    ') Do @Echo %%G) 1>"%TEMP%\product-list.txt"

Now to work on your question, using the output of the file produced above, and using the exact same methodology on the target PC's:

The following batch file should identify all Product Names which do not match those in the base list:

@(Set LF=^
% 0x0A %
)

@For /F Delims^=^ EOL^= %%H In ('(
 For /F Tokens^^^^^^^=6^^^^^^^ Delims^^^^^^^=^^^^^^^"^^^^^^^ EOL^^^^^^^= %%G In
 ('%SystemRoot%\System32\wbem\WMIC.exe Product Get Name /Format:MOF'^) Do @Echo
 %%G^^^%%LF^^^%%Rem.^)^| %SystemRoot%\System32\findstr.exe
 /XVLIG:"%TEMP%\product-list.txt"'
) Do @Echo %%H

@Pause

So once you're satisfied with the resulting output of the above script, just change Echo %%H to %SystemRoot%\System32\wbem\WMIC.exe Product Where "Name='%%H'" Call Uninstall


Additional Note: You should be aware that there may be characters included in some of those name strings which do not render correctly in the codepage for your running batch file, or may not be understood in the WMIC.exe WHERE clause. Such things are outside of the scope of your question, and have not been catered for in the above answer.

Compo
  • 36,585
  • 5
  • 27
  • 39