Within PowerShell, this is very easy to do.
The below block of script will take a computer name, your username and password, connect to the remote computer and list all installed software by name:
$computerName = "SomeComputerName"
$yourAccount = Get-Credential
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
Get-WmiObject Win32_Product | Select Name
}
When you have the name of the product you want to uninstall remotely - you can the perform an uninstall like this:
$computerName = "SomeComputerName"
$appName = "AppName"
$yourAccount = Get-Credential
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
Get-WmiObject Win32_product | Where {$_.name -eq $appName} | ForEach {
$_.Uninstall()
}
}
In the above eaxmples - replace "SomeComputerName" with the name of the computer you wish to uninstall from.
You can also make the script prompt you for a computer name if you prefer with the following line:
$computerName = Read-Host "Enter Computer Name"
If you have multiple computers with the same piece of software that you want to uninstall - you can also define an array of computers to work with and do uninstalls from lots of machines:
$computerNames = @("SomeComputerName1", "SomeComputerName2", "SomeComputerName3")
$appName = "AppName"
$yourAccount = Get-Credential
ForEach ($computerName in $computerNames) {
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
Get-WmiObject Win32_product | Where {$_.name -eq $appName} | ForEach {
$_.Uninstall()
}
}
}
Answer from Fazer87 on Stack ExchangeWithin PowerShell, this is very easy to do.
The below block of script will take a computer name, your username and password, connect to the remote computer and list all installed software by name:
$computerName = "SomeComputerName"
$yourAccount = Get-Credential
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
Get-WmiObject Win32_Product | Select Name
}
When you have the name of the product you want to uninstall remotely - you can the perform an uninstall like this:
$computerName = "SomeComputerName"
$appName = "AppName"
$yourAccount = Get-Credential
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
Get-WmiObject Win32_product | Where {$_.name -eq $appName} | ForEach {
$_.Uninstall()
}
}
In the above eaxmples - replace "SomeComputerName" with the name of the computer you wish to uninstall from.
You can also make the script prompt you for a computer name if you prefer with the following line:
$computerName = Read-Host "Enter Computer Name"
If you have multiple computers with the same piece of software that you want to uninstall - you can also define an array of computers to work with and do uninstalls from lots of machines:
$computerNames = @("SomeComputerName1", "SomeComputerName2", "SomeComputerName3")
$appName = "AppName"
$yourAccount = Get-Credential
ForEach ($computerName in $computerNames) {
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
Get-WmiObject Win32_product | Where {$_.name -eq $appName} | ForEach {
$_.Uninstall()
}
}
}
If you create a file called "servers.txt" and place your list of servers in it you could also reference $computerNames as follows:
$computerNames = Get-Content "C:\some-directory\servers.txt"
$appName = "AppName"
$yourAccount = Get-Credential
ForEach ($computerName in $computerNames) {
Invoke-Command -ComputerName $computerName -Credential $yourAccount -ScriptBlock {
Get-WmiObject Win32_product | Where {$_.name -eq $appName} | ForEach {
$_.Uninstall()
}
}
}
I've used this approach many times in production environments and it seems to work for me. Always test this in a non-production environment before completing in a prod environment.
So I’m having trouble uninstalling software through powershell. I am an admin and I can uninstall software that has an uninstall string that’s similar to {numbers letters}
But when I try to uninstall software that’s uninstall string is a folder in program files (x86). It won’t work. Here is my script:
$computername = name $number = identifying number $software = get-wmiobject win32_product -Computer name $computername | where-object {$_.IdentifyingNumber -eq $Number} If ($software){ $software.uninstall () }else{ $Number + ‘is not installed on’ + $computername
How can I change this to use the software without the identifying number?
Uninstall Application from Remote PC with Powershell Invoke Command - Programming & Development - Spiceworks Community
Uninstalling a remote application not working
Uninstall Software Remote Powershell Script - Stack Overflow
Powershell Script to Uninstall Applications Remotely - Programming & Development - Spiceworks Community
Videos
I have been working this for a couple days now, and no matter how I run this and work it, it seems to uninstall the program via PowerShell and returns the success code:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :This happens with various notoriously difficult to remove software such as McAfee.
The command being used is:
Get-WmiObject -Class win32_product -Filter "Name like '%McAfee%'" | ForEach-Object {$_.Uninstall()}I've tried various scripts, solutions on StackOverflow, and variations and nothing works.
Hi there,
I’m trying to put together a Powershell GUI script for uninstalling software on network computers remotely using WMI. The form stuff seems to be OK, the buttons are generating properly, but I’m having a hard time kicking off the function. The idea is that it passes the name of the button over to:
Get-WmiObject Win32_Product -ComputerName $ComputerName | Where-Object{$_.name -match *name on button* } | ForEach-Object{$_.Uninstall()}
It doesn’t seem to do it though, I’m pretty sure the value is ending up being null. I guess the best way to do this would be to have the button called the software name and have the button click pass the software’s IdentifyingNumber to the command, but I can’t figure out how to do that either.
Here’s what I have so far:
$ComputerName = $env:computername #Temporarily using my computer for testing
$form = New-Object System.Windows.Forms.Form
$Software = Get-WmiObject Win32_Product -ComputerName $computername | Select Name | Sort-Object -property name #Get list of apps and alphabetise them
$Form_i=0
$App_to_remove = $null #Make sure button variable starts empty
foreach ($App in $Software){
if ($App.Name -ne $null){ #Weed out the empty buttons
$AppButton = New-Object System.Windows.Forms.Button
$AppButton.Name = $App.name
$AppButton.location = "10,$(25*++$Form_i)"
$AppButton.size = '400,20'
$AppButton.Text = $App.name
# Add Click Event
$AppButton.add_click({
$App_to_remove = $this.Content.trim()
Get-WmiObject Win32_Product -ComputerName $ComputerName | Where-Object{$_.name -match $App_to_remove } | ForEach-Object{$_.Uninstall()}
})
$form.controls.add($AppButton)
}
}
#Stuff to format the form
$form.HorizontalScroll.Maximum = 0
$form.AutoScroll = $false
$form.VerticalScroll.Visible = $false
$form.AutoScroll = $true
$Form.minimumSize = New-Object System.Drawing.Size(450,500)
$Form.maximumSize = New-Object System.Drawing.Size(450,500)
$Form.Text = "Uninstall Apps"
$Form.ShowIcon = $False
$form.showdialog()
I think I need to build a function for the click, but I can’t figure out how to pass information from the button to the function, whether it be the preferred IdentifyingNumber or just simply the name on the button.
Any advice on how to get this working would be appreciated.
Cheers.
Make it work first, then worry about GUI… PowerShell is not super for GUI stuff…
Here's another approach, using Schedule Tasks.
Create a task locally using the logged-on user credentials, you must have admin rights on the remote computer of course.
Function Run-RemoteCommand
{
Param(
[Parameter(Mandatory = $true)]
[String]
$Computer,
[Parameter(Mandatory = $true)]
[String]
$Task,
[Switch]
$Background
)
$TaskName = "TempTask"
$Explorer=gwmi win32_process -ComputerName $Computer | ? {$_.ProcessName -eq "explorer.exe" }
$OWner = $Explorer.GetOwner().Domain + "\" + $Explorer.GetOwner().User
if ($Background)
{
$Create = "SCHTASKS /Create /TN ""$TaskName"" /RU System /TR ""$Task"" /SC Once /ST 22:30"
}
Else
{
$Create = "SCHTASKS /Create /TN ""$TaskName"" /RU $Owner /TR ""$Task"" /SC Once /ST 22:30"
}
$Runit = "SCHTASKS /Run /TN ""$TaskName"""
$Delete = "SCHTASKS /Delete /TN ""$TaskName"" /F"
$WMI = Get-WmiObject -List Win32_Process -ComputerName $Computer
$WMI.Create($Create)
$WMI.Create($Runit)
Sleep 2
$WMI.Create($Delete)
}
To Run it:
Run-RemoteCommand -Computer "192.xxx.xxx.xxx" -Task 'c:\path\uninstall.exe /S'
if you want to run it as 'NT AUTHORITY\SYSTEM' add the -Background Switch
The best way would be to always have the local shell run as an elevated user. This allows you to start invoke-command without providing credentials in a script.
Once you have a Powershell running as an administrative user, and you can run your script using invoke-command. I do it all the time.
If you are scheduling this task, you will have to have the task run as the elevated user. This again, stops you from having to put the password in plain text in the script.