Search code examples
windowsbatch-filewmic

"wmic product call uninstall" EXCEPT for specified criteria?


I seem to have hit a road block on the uninstall portion of a cleanup batch script. I know the actual command is call uninstall but I'm using get name to try and see if WMIC correctly identifies the programs I need deleted.

I have a list of programs from wmic product get name run on a freshly installed PC, and I'm using that as a base and trying to make it uninstall everything that is NOT found on the list.

Here's what I've got so far:

set /p pl=<"%temp%\product-list.txt"
wmic product where NOT name=%pl% call uninstall

I try to test it with get name instead, since I don't want to actually uninstall all of my utilities and such that users outside of my department don't need, and there seems to be a sort of syntax error:

C:\WINDOWS\system32>wmic product where NOT name=%pl% get name
name - Invalid alias verb.

I'm pretty sure I'm approaching the code incorrectly, I just don't know the correct way to achieve this. Perhaps it's correct, just not compatible syntax with get name, but I somehow doubt it. Any help is appreciated.


Solution

  • The gotcha here is that the query has to be a single argument. Since you have a space there (between NOT and name=%pl%), you will have to enclose the whole query in doublequotes, otherwise it will be looked at as two arguments. That's why you get the error - because WMIC thinks that NOT is the query and name would then be a verb like get, following the where clause.

    wmic product where "NOT name=%pl%" get name
    

    Just in case you then encounter another error stating it's an invalid query: In this case the issue is that the name string you are comparing with (in the pl variable) is not enclosed in single quotes - you could fix that like this:

    wmic product where "NOT name='%pl%'" get name
    

    (Note: Instead of NOT name='%pl%', you could also use name<>'%pl%', but the quotes would still be required, this time not because of spaces but because < and > would otherwise have special meaning to the shell.)