1

I have a bunch of servers I have to uninstall an app from. I am using:

$app = Get-WmiObject -Class Win32_Product | Where-Object { 
    $_.Name -match "application name" 
}

$app.Uninstall()

I have tested the above out and it works great. I want to now run this so it uninstalls the app on a bunch of servers. I know I can use the for each option but for some reason I am having an issue getting it to work.

I created a text file called servers and listed my servers in there but it errors out each time.

Does anyone have a good pay to add a for each part to my above uninstall portion so it works?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
aztech
  • 71
  • 2
  • 5
  • 9

3 Answers3

0

Later on the page that you got your original code from, there is an answer from David Setler that does what you need. Slightly modified to fir your scenario:

$computers = Get-Content C:\servers.txt
foreach($server in $computers){
    $app = Get-WmiObject -Class Win32_Product -computer $server | Where-Object { 
        $_.Name -match "application name" 
    }
    $app.Uninstall()
}

Assuming $computers is the list of servers from your servers text file.

Community
  • 1
  • 1
Samuel Prout
  • 655
  • 5
  • 16
  • If I use this one how does it know where my list of servers are? I see it says $server in $computer but I don't see a reference to where my text file is. In the top response I see it shows a path to the text file. – aztech Jun 08 '15 at 23:52
  • Updated to add the `$computers` variable. Just fix the path to your file. – Samuel Prout Jun 09 '15 at 00:14
0

The -ComputerName parameter of Get-WmiObject accepts a list of computernames.

$servers = Get-Content 'C:\your\computerlist.txt'

Get-WmiObject -Computer $servers -Class Win32_Product |
  ? { $_.Name -like '*application name*' } |
  % { $_.Uninstall() }
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

I would stay away from win32_product because this class isn't working properly. See https://support.microsoft.com/en-us/kb/974524. Basicly what happens when you query this class is that every msi will trigger a repair an the software package (spamming the eventlog) which in some cases can screw with installed software (I've seen broken installations of some programs after querying this class - though it's rare, you don't want to risk this on production servers).

bluuf
  • 936
  • 1
  • 6
  • 14