How To Remotely Uninstall and Install A Program using PowerShell

1

If you don’t have a software deployment tool, such as PDQ Deployment, uninstalling programs from a remote computer could be quite painful. And once again, here is where PowerShell shines.

First of all, let’s see how to get a list of installed programs on a remote computer:

Get-WmiObject Win32_Product -ComputerName $computername | Select-Object -Property IdentifyingNumber, Name
image 5 600x245 - How To Remotely Uninstall and Install A Program using PowerShell

To find a specific program installed on a remote computer:

Get-WmiObject Win32_Product -ComputerName $computername | Where-Object {$_.IdentifyingNumber -eq $number}

Now, let’s uninstall that program.

(Get-WmiObject Win32_Product -ComputerName $computername | Where-Object {$_.IdentifyingNumber -eq $number}).Uninstall()

To avoid the error message when trying to uninstall a non-existed program.

$ComputerName = 'Computer'
$number = '{AC76BA86-1033-FFFF-7760-0E1108756300}' #Adobe Acrobat ID
$adobe = Get-WmiObject Win32_Product -ComputerName $ComputerName | Where-Object {$_.IdentifyingNumber -eq $number}
if ($adobe) {
  $adobe.Uninstall()
}
else {
  $number + ' is not installed on ' + $ComputerName
}

How about installing a program on a remote computer? That’s a bit more complicated and depend on the install package. If it’s an MSI package or a package that has a silent option, PowerShell should be able to take care of it.

If the install package is already on the remote computer,

Invoke-Command -ComputerName $computer -ScriptBlock {
  Start-Process 'c:\temp\setup.exe' -ArgumentList '/slient' -Wait
}

But if not, you will need to copy the package over first and then run the installation process.

Here is a more complete example:

$computer = 'computer1'
$session = New-PSSession -ComputerName $computer
Copy-Item -Path '\server\software\sophosetup.exe' -ToSession $session -Destination 'c:\windows\temp\sophossetup.exe' -Force
Invoke-Command -Session $session -ScriptBlock   {
   Start-Process 'c:\windows\temp\sophosSetup.exe' -ArgumentList '--quiet' -Wait
   Move-Item -Path 'c:windows\temp\sophossetup.exe'    
} 

I took the Sophos software as an example but you should get the idea.

Finally, thought to mention that in order to get the PowerShell scripts to work on remote computers there are two prerequisites that need to be met.

  • WinRM needs to be enabled on the remote computer
  • You need proper credentials to run the script on the remote computer.
SHARE

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here