tags:

views:

3159

answers:

4

Is there a simple way to hook into the standard 'Add or Remove Programs' functionality using PowerShell to uninstall an existing application? Or to check if the application is installed?

+12  A: 
$app = Get-WmiObject -Class Win32_Product | Where-Object { 
    $_.Name -match "Software Name" 
}

$app.Uninstall()

Edit: Rob found another way to do it with the Filter parameter:

$app = Get-WmiObject -Class Win32_Product `
                     -Filter "SELECT * FROM Win32_Product WHERE Name = 'Software Name'"

Your mileage my vary, but I can't get this version to work on my machine. I get this error (with what I think is a valid 'Software Name'):

Get-WmiObject : Invalid query

Jeff Hillman
After a bit of research you can also use the -filter clause of Get-WmiObject:$app = Get-WmiObject -Class Win32_Product -filter "select * from Win32_Product WHERE name = 'Software Name'"
Rob Paterson
This WMI class takes FOREVER to enumerate. I suggest Jeff that you update your code to include Rob's tip.
halr9000
I agree with Jeff... my version doesn't seem to be working now....
Rob Paterson
This WMI class is only available after being installed. Its not default part of XP or Windows 2003. Its default on Vista/2008
James Pogran
If you get a "generic failure", this hotfix http://support.microsoft.com/kb/970553/ may help
David Gardiner
+1  A: 

Jeff Hillmans answer is pretty much it, I would say that it may be better to use IdentifyingNumber rather than the name, just in case.

Tubs
+1  A: 

Note that looking at WMI will only work for products that were installed via an MSI.

EBGreen
+6  A: 

To fix up the second method in Jeff Hillman's post, you could either do a:

$app = Get-WmiObject 
            -Query "SELECT * FROM Win32_Product WHERE Name = 'Software Name'"

Or

$app = Get-WmiObject -Class Win32_Product `
                     -Filter "Name = 'Software Name'"
Robert Wagner