This article shows how to remotely uninstall software using PowerShell. I will explain how to uninstall one application on a PC, run a simple cmdlet for a remote PC, and finally, uninstall one application from a remote PC step by step.
Table of Contents
Uninstall an Application from a PC
Get-WmiObject Cmdlet gets instances of WMI classes or information about the available classes. I could retrieve a list of applications using it.
As an example, I tried to uninstall “Node.js” from a PC locally.
$AppToRemove = "Node.js"
Get-WmiObject Win32_product | Where {$_.name -eq $AppToRemove} | ForEach {
$_.Uninstall()
}
Run:
Run a Command remotely with Invoke-Command
Invoke-Command is to run Command on local or remote Computers.
The credential is used to access Remote Computer, and the user should have enough Privilege to uninstall App. If you are using the same credential of your current session, you may not use the credential parameter.
$adminCredential = Get-Credential
$remoteComputer = "PC1"
Invoke-Command -ComputerName $remoteComputer -Credential $adminCredential -ScriptBlock {
IPConfig -all
}
Please note that you can also store a credential into an XML file so that you can use the credential without entering again.
# To store
Get-Credential | Export-Clixml "radmincredentials.xml"
# To read
$adminCredential = Import-Clixml "radmincredentials.xml"
Uninstall an Application Remotely
Let’s try to uninstall an Application remotely using the above functions.
$PC = "PC1"
$adminCredential = Get-Credential
Invoke-Command -ComputerName $PC -Credential $adminCredential -ScriptBlock {
$AppToRemove = "Node.js"
Get-WmiObject Win32_product | Where {$_.name -eq $AppToRemove} | ForEach {
$_.Uninstall()
}
}
Run:
Uninstall an Application Remotely from Multiple Computers
You can extend the function to multiple Remote PC cases with an array of computer names.
$PCs = @("PC1", "PC2", "PC3")
$adminCredential = Get-Credential
ForEach ($pc in $PCs) {
Invoke-Command -ComputerName $pc -Credential $adminCredential -ScriptBlock {
$AppToRemove = "Node.js"
Get-WmiObject Win32_product | Where {$_.name -eq $AppToRemove} | ForEach {
$_.Uninstall()
}
}
}
Alternatively, you can also use a list of remote PCs by reading the file containing the list.
$adminCredential = Get-Credential
Invoke-Command -ComputerName (Get-Content D:\pcNamelist.txt) -Credential $adminCredential -ScriptBlock {
$AppToRemove = "Node.js"
Get-WmiObject Win32_product | Where {$_.name -eq $AppToRemove} | ForEach {
$_.Uninstall()
}
}
Refer to other articles: