Uninstalling a program of many computers efficiently

4.9k views Asked by At

I have a PowerShell that would uninstall a program from remote computers but it takes a long time since it goes through all the computers in the list. I just need help from you to modify it so it first checks if the program exists on the remote computer or not and then uninstalls it:

$comps = gc "C:\Computers.txt"
$appname = gc "C:\appname.txt"
foreach($comp in $comps){
   foreach ($appname in $appname){
      $prod=gwmi -computer $comp win32_product  | ?{$_.name -eq "$appname"}
      $prod.uninstall()
   }
}
2

There are 2 answers

0
user3314399 On BEST ANSWER

Got it! thanks

$comps= gc "C:\Computers.txt"
$appname = gc "C:\appname.txt"
foreach($comp in $comps){
$program = gwmi -computer $comp Win32_Product | sort-object Name | select Name | where { $_.Name -match “$appname”}
if($program -eq $null)
{
 Write-host "Does Not"
}
else
{
$prod=gwmi -computer $comp win32_product  | ?{$_.name -eq "$appname"}
$prod.uninstall()
}
}
3
Jonathan Turpie On

Try using foreach –parallel instead of just foreach. Official docs here.

workflow uninstallstuff {
    sequence {
        $comps = gc "C:\Computers.txt"
        $appname = gc "C:\appname.txt"
        foreach -parallel ($comp in $comps){
            foreach ($appname in $appname){
                $prod=gwmi -computer $comp win32_product  | ?{$_.name -eq "$appname"}
                $prod.uninstall()
            }
        }
    }
}

That should run each computer in parallel but each app for that computer will be uninstalled sequentially.

edit: rewrote as a workflow. I haven't tested it yet.