How To Remotely Uninstall and Install A Program using PowerShell

11

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.

11 COMMENTS

  1. hello, this commands only works on virtual machine? but does not work on physical (localy)
    Get-WmiObject : Generic failure
    At line:1 char:1
    + Get-WmiObject Win32_Product | Select-Object -Property IdentifyingNum …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], Managemen
    tException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.C
    ommands.GetWmiObjectCommand

  2. Line 6 — Move-Item -Path ‘c:windows\temp\sophossetup.exe’ need to be corrected to Move-Item -Path ‘c:\windows\temp\sophossetup.exe’ — you forgot the \ after c:.

    • Assign an array to the text file

      $computers = get-content c:\computers.txt

      Then nest the above script in a for loop

      foreach ($computer in $computers)
      {do stuff script}

  3. Using the win32_product class in this script will only return (and be able to un-install) apps that were installed using an .msi. If they are not, win32_product will not even see them as being installed.

  4. Sir thanks for useful info but when i m trying to uninstall an application i m getting Return Value 1603

    Please guide
    __GENUS : 2
    __CLASS : __PARAMETERS
    __SUPERCLASS :
    __DYNASTY : __PARAMETERS
    __RELPATH :
    __PROPERTY_COUNT : 1
    __DERIVATION : {}
    __SERVER :
    __NAMESPACE :
    __PATH :
    ReturnValue : 1603
    PSComputerName :

LEAVE A REPLY

Please enter your comment!
Please enter your name here