PowerShell: Uninstall programs on a remote PC

URL dieses Beitrags: http://blog.stefan-macke.com/2010/10/01/powershell-uninstall-programs-on-a-remote-pc/

Today I wanted to roll out Microsoft Office 2010 to a few clients but I needed to get rid of old Office installations on these systems because those could lead to problems while installing the new version. Instead of uninstalling them manually I wrote the following short PowerShell script UninstallOffice.ps1 for the task, which takes a computer name as its only parameter.

  1. Param($pc = "");
  2.  
  3. if ($pc -eq "")
  4. {
  5.   write-host ("Usage: UninstallOffice.ps1 PC") -foregroundcolor "red";
  6.   exit;
  7. }
  8.  
  9. $programsToUninstall = @(
  10.   "Microsoft Office Word Viewer 2003",
  11.   "Microsoft Office Excel Viewer 2003",
  12.   "Microsoft Office PowerPoint Viewer 2003",
  13.   "Microsoft Office Visio Viewer 2003 (Deutsch)",
  14.   "Microsoft Office XP Professional",
  15.   "Microsoft Office Standard Edition 2003",
  16.   "Microsoft Office Professional Edition 2003",
  17.   "Microsoft Office Professional Plus 2007"
  18. );
  19.  
  20. $products = get-wmiobject -class "Win32_Product" -namespace "root\CIMV2" -ComputerName $pc;
  21. if ($products.length -gt 0)
  22. {
  23.   $products | % {
  24.     if ($programsToUninstall -contains $_.caption)
  25.     {
  26.       write-host ("Uninstalling " + $_.caption + " on " + $pc) -foregroundcolor "red";
  27.       $_.uninstall();
  28.     }
  29.   };
  30. }

You could replace the array $programsToUninstall with a list of the programs you would like to uninstall. The following snippet gives you a list of the installed programs’ names you could use in the array.

  1. get-wmiobject -class "Win32_Product" -namespace "root\CIMV2" -ComputerName $pc | select caption

Update: If you get an error message when trying to uninstall the software, you may want to install this hotfix from Microsoft: Error message when you query Win32_Product class after you install applications by using Microsoft Windows Installer (MSI) 4.5 with “Per-User” option in Windows XP: “0×80041001 – Generic failure”.

Kommentare zu diesem Beitrag

  1. Gravatar Thomas
    Am 6. September 2011 um 07:59 Uhr

    Ich bin in einer ähnlichen Situation und hoffe du hast evtl. die korrekte Syntax bzw. ein funktionierendes Skript.

    Ich möchte alle Softwareprodukte, die den Namen soft123 enthalten von allen PCs deinstallieren, die pc im Computernamen haben (soft123 v.1.0, soft123 2 deinstallieren von pc01, pc02 usw.).

    Würde mich freuen wenn du mir weiterhelfen kannst. Habe es schon in div. Foren versucht aber bislang ohne Erfolg.

  2. Gravatar Stefan
    Am 6. September 2011 um 08:04 Uhr

    Hallo Thomas. Das sollte kein großes Problem darstellen. Die PCs kannst du z.B. mit diesem Script ermitteln: Active Directory mit der PowerShell auslesen. Die Einschränkung auf pc im Namen sollte recht einfach sein. Dann musst du nur noch das obige Script leicht anpassen, z.B. aus Zeile 24 das hier machen:
    if ($_.caption -match “soft123″)

  3. Gravatar Stefan
    Am 6. September 2011 um 13:02 Uhr
    $pcNameMustMatch = "pc145";
    $programNameMustMatch = "soft123";
    
    function GetComputersFromLDAP()
    {
    	trap
    	{
    		write-host ("Error while retrieving computers from LDAP: " + $_.exception.message) -foregroundcolor "red";
    		return $false;
    	}
    	$pcs = @();
    	$ldapSearcher = new-object directoryservices.directorysearcher;
    	$ldapSearcher.filter = "(objectclass=computer)";
    	$computers = $ldapSearcher.findall();
    	foreach ($computer in $computers)
    	{
    		$pc = "" | select-object Name;
    		$pc.Name = $computer.properties["name"];
    		$pcs += $pc;
    	}
    	return ($pcs | sort-object Name);
    }
    
    $pcs = GetComputersFromLDAP;
    foreach ($pc in $pcs)
    {
    	write-host "found computer" $pc.Name;
    	if ($pc.Name -notmatch $pcNameMustMatch)
    	{
    		write-host ("  -> name does not match " + $pcNameMustMatch) -foregroundcolor "red";
    		continue;
    	}
    
    	write-host ("  -> name matches " + $pcNameMustMatch) -foregroundcolor "green";
    	write-host ("  -> searching for installed software");
    	
    	$products = get-wmiobject -class "Win32_Product" -namespace "root\CIMV2" -ComputerName $pc.Name;
    	if ($products.length -gt 0)
    	{
    		$products | % {
    			if ($_.caption -match $programNameMustMatch)
    			{
    				write-host ("  -> uninstalling " + $_.caption + " on " + $pc.Name) -foregroundcolor "yellow";
    				#$_.uninstall(); # uncomment this line if you really want to uninstall the software!
    			}
    		};
    	}
    	else
    	{
    		write-host ("  -> no software found on" + $pc.Name) -foregroundcolor "red";
    	}
    
    }
  4. Gravatar Markus
    Am 30. Mai 2012 um 10:44 Uhr

    Hey Stefan,

    danke für die guten Skripte. Mitlerweile bin ich auch bei Powershell angelangt und bin durch eine Suche (mal wieder) auf deinen Blog gestoßen ;)

    Gruß

    Markus

Einen Kommentar schreiben

XHTML: Diese Tags sind erlaubt: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>