1

I have a user who works from home, but he's connected to our internal network. I have a remote access program (LANDESK) that I can connect to his system, but per our office policy, I can't remove programs unless I'm logged into my special admin account. Switching accounts disconnects this user from the VPN, effectively kicking me off his system.

So as a fairly n00b PowerShell user, I'm attempting to come up with a means to run a command to execute the uninstaller for the software as my admin account while he's still logged in. Preferably silently. Windows 10 v1803. Software is SSMS 2014 if that helps.

Thanks for suggestions.

1
  • You can find the uninstall string in the registry and then execute it remotely using psexec or powershell remoting if you prefer. Aug 16, 2019 at 19:37

2 Answers 2

1

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()
        }
    }
}
5
  • For clarification, the quotes for things like "SomeComputerName" are meant to be filled in with actual info, correct? Aug 21, 2019 at 19:43
  • absolutely... unless you happen to have a computer called SomeComputerName ;) I'll update my answer
    – Fazer87
    Aug 22, 2019 at 9:01
  • Running a test on my neighbor's system gets an error: Invalid class "Win32_Procuct" + CategoryInfo : InvalidType: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand Aug 22, 2019 at 17:54
  • I think I see it. I think Product was misspelled Aug 22, 2019 at 18:30
  • 1
    sorry about that - I wrote it in a rush... I started with the complete solution and then worked backwards to the simple audit and made a typo when stripping it back and re-keying bits. I've edited the answer to stop others having this issue
    – Fazer87
    Aug 22, 2019 at 22:02
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.