batch-filecmdwmic

WMIC product get name in a FOR loop: NOT was unexpected at this time


"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.


Solution


  • 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.