Murali Manohar
Murali Manohar

Reputation: 613

Uninstall a product from powershell

How to uninstall a product using the guid of the product

I tried this

msiexec /x {guid}

But i cant uninstall the product

It worked fine when i use

Wmic product where identifyingnumber={guid} call uninstall

Upvotes: 1

Views: 6509

Answers (3)

drhunn
drhunn

Reputation: 21

$ComputerName = Read-Host "Computer Name"
$Program= Read-Host "Program"
$apps = Get-WmiObject -Class Win32_Product -ComputerName $ComputerName | where name -Like "$Program*"
$index = 0

foreach ($app in $apps){
  $index++
  $index
  $app

}

$uninstallIndex = Read-Host "Please enter the number you want to uninstall"

$apps[$uninstallIndex-1].uninstall()

$apps = Get-WmiObject -Class Win32_Product -ComputerName $ComputerName |
where name -Like "$Program*"

foreach ($app in $apps){
  $index++
  $index
  $app

}

Upvotes: 0

Joey
Joey

Reputation: 354506

You need to quote the argument. Note that PowerShell uses braces as part of its syntax (to define script blocks), so passing them to a native command does ... unexpected things (from the native command's perspective):

PS Home:\> args {foo}
argv[0] = H:\Batches\args.cmd
argv[1] = -encodedCommand
argv[2] = ZgBvAG8A
argv[3] = -inputFormat
argv[4] = xml
argv[5] = -outputFormat
argv[6] = text

PowerShell apparently tries to support calling powershell { statements } in a way that won't break. And in the process causes lots of unexpected input to native commands that don't happen to be PowerShell.

Note that quoting solves this:

PS Home:\> args '{foo}'
argv[0] = H:\Batches\args.cmd
argv[1] = {foo}

Also there's the way via WMI that Avshalom mentions.

Upvotes: 3

Avshalom
Avshalom

Reputation: 8889

$WMI = Get-WmiObject win32_product -Filter 'IdentifyingNumber = "{guid}"'
$WMI.Uninstall()

Upvotes: 8

Related Questions