Friday, January 20, 2012

Uninstall Software using PowerShell

 

Hi,

today i was trying to uninstall the the software using PowerShell.

i See few blog posts about uninstalling software using PowerShell they re recommending WMI to uninstall software using the .Uninstalled() method. But i think this method is not right because it don’t un-installed the software properly. Sometime uninstalling software using these method cause system/laptop to crash.

In my view, un-installation should be done by proper way.

Lets un-install winzip in a proper way. You can also script it.

we are going to use WMI but in another way.

Lets Start

lets see installed software first using the below command.

Get-WmiObject -Class win32_product

it is showing the list of all installed software.

 20-01-2012 17-24-32

Now see if we have winzip installed. we are piped the previous command to Where-Object and searching for name which contain winzip in it.

Get-WmiObject -Class win32_product | where { $_.Name -like "*Winzip*"}

Yes we have WinZip installed

20-01-2012 17-27-40 

Lets pipe the above command to Format-List to that we can see all the members of the Winzip.

Get-WmiObject -Class win32_product | where { $_.Name -like "*Winzip*"} | format-List *

This will give us  all the member properties of Winzip. The property in which we are interested is “LocalPackage”

20-01-2012 17-31-01

and you can see that install LocalPackage contains a path to a .msi executable file. and we can uninstall .msi files uisng MSIEXEC. lets check the which command line switches MSIEXEC support.

Simple run MSIEXEC and it will show you all the switches msiexec support.  The switch which we are going to use is /x and /passive

20-01-2012 17-38-01

lets combine the all commands together. I put the first command in to $Wzip variable.

  1. $Wzip = Get-WmiObject -Class win32_product | where { $_.Name -like "*Winzip*"}
  2. $Wzip.localPackage

Now try to run $Wzip.localPackage and this will show us the WinZip install source

20-01-2012 17-43-54 

Run the below command to uninstall the Winzip.

msiexec /x  $wzip.localpackage /passive

this will start un-installation of Winzip and will show only the Progress bar only {because we are using msiexex’s /passive switch”

Here it start uninstallation .

20-01-2012 18-05-01 

20-01-2012 18-05-38 

20-01-2012 18-05-44 

20-01-2012 18-06-37 

All done !!!! lets check Programs and features in Control Panel and winzip is not here :)

20-01-2012 18-11-05 

 

You are save all this in script and use it to uninstall winzip or any other software using powershell.

Hope it helps someone :)

 

Thanks

Aman Dhally

 

3 comments:

  1. Hi Aman, I have been doing some research about this. Win32_product unfortunately is not the recommended class to use. It's very very slow and when you query it, it causes your application error log to fill up like crazy with the following error messages :
    "Windows Installer reconfigured the product. Product Name: Debugging Tools for Windows (x86). Product Version: 6.12.2.633. Product Language: 1033. Manufacturer: Microsoft Corporation. Reconfiguration success or error status: 0."

    Even Microsoft doesn't recommend it.

    "Win32_product Class is not query optimized. Queries such as “select * from Win32_Product where (name like 'Sniffer%')” require WMI to use the MSI provider to enumerate all of the installed products and then parse the full list sequentially to handle the “where” clause. This process also initiates a consistency check of packages installed, verifying and repairing the install. With an account with only user privileges, as the user account may not have access to quite a few locations, may cause delay in application launch and an event 11708 stating an installation failure.

    Win32reg_AddRemovePrograms is a much lighter and effective way to do this, which avoids the calls to do a resiliency check, especially in a locked down environment. So when using Win32reg_AddRemovePrograms we will not be calling on msiprov.dll and will not be initiating a resiliency check. "

    http://support.microsoft.com/kb/974524

    I have seen similar messages on my Windows 7.
    Just a word of advice.

    I am working on one of my project to push out patches and hence the last step is to devise a method to roll back the patches. Hence i am trying to find a way to roll back the patch remotely, pretty much the same way, I push it. so researching.. thanks for the article.

    ReplyDelete
  2. Hi Punnet

    Yes i know about it and actually i was about to add a note for the same.

    thanks for the advise dear :o)

    thanks
    aman

    ReplyDelete
  3. Very helpful.. It works perfectly fine :)

    ReplyDelete