5

I am trying to write a mini w32 executable to remotely uninstall an application using WMI.

I can list all the installed applications using this code below but i couldnt find a way to uninstall the application remotely thru WMI and C#

I know I can do same using msiexec as a process but I wish to solve this using WMI if its possible...

Thanks, Cem

static void RemoteUninstall(string appname)
{
    ConnectionOptions options = new ConnectionOptions();
    options.Username = "administrator";
    options.Password = "xxx";
    ManagementScope scope = new ManagementScope("\\\\192.168.10.111\\root\\cimv2", options);
    scope.Connect();


    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Product");

    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection queryCollection = searcher.Get();

    foreach (ManagementObject m in queryCollection)
    {
        // Display the remote computer information

        Console.WriteLine("Name : {0}", m["Name"]);

        if (m["Name"] == appname)
        {
            Console.WriteLine(appname + " found and will be uninstalled... but how");
            //need to uninstall this app...
        }
    }

}
Nooneelse
  • 127
  • 2
  • 11
  • Win32_Product class has a uninstall method, and there are few c++ and powershell examples wandering in net, however I couldn't find a single example nor documentation using win32_product class in c#. – Nooneelse Aug 26 '10 at 17:08
  • By the way, there're at least two other questions (with sample code) about uninstalling remote applications using WMI: http://stackoverflow.com/questions/2390268/using-wmi-to-uninstall-programs and http://stackoverflow.com/questions/327650/wmi-invalid-class-error-trying-to-uninstall-a-software-on-remote-pc – Helen Aug 26 '10 at 20:47
  • Sorry for taking your time, i guess i was a little tired and distracted while searching for an answer. anyway, thanks for your help. – Nooneelse Aug 27 '10 at 16:27

1 Answers1

15

Have a look at WMI Code Creator (a free tool from Microsoft) — it can generate WMI code for you in various languages, including C#.

Here's an example illustrating the Win32_Product.Uninstall method usage. You need to know the GUID, name and version of the application you want to uninstall, as they are the key properties of the Win32_Product class:

...

ManagementObject app = 
    new ManagementObject(scope, 
    "Win32_Product.IdentifyingNumber='{99052DB7-9592-4522-A558-5417BBAD48EE}',Name='Microsoft ActiveSync',Version='4.5.5096.0'",
    null);

ManagementBaseObject outParams = app.InvokeMethod("Uninstall", null);

Console.WriteLine("The Uninstall method result: {0}", outParams["ReturnValue"]);

If you have partial info about the application (e.g. only name or name and version), you can use a SELECT query to obtain the corresponding Win32_Process object:

...
SelectQuery query = new SelectQuery("Win32_Product", "Name='Microsoft ActiveSync'");

EnumerationOptions enumOptions = new EnumerationOptions();
enumOptions.ReturnImmediately = true;
enumOptions.Rewindable = false;

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query, options);

foreach (ManagementObject app in searcher.Get())
{
    ManagementBaseObject outParams = app.InvokeMethod("Uninstall", null);

    Console.WriteLine("The Uninstall method result: {0}", outParams["ReturnValue"]);
}
Helen
  • 87,344
  • 17
  • 243
  • 314