Automate uninstalling software from remote servers

Hello,

I have a working code to uninstall Java application from a remote server manually.
I would like to automate the task, please can i get help to fix the code.

Working code

Enter-PSSession -ComputerName pwgsascs575000g

$apps = Get-WmiObject -Query “SELECT * FROM Win32_Product Where Vendor LIKE ‘%ORACLE%’ AND Name like ‘%Java%’”

foreach ($app in $apps)
{
$app.uninstall()
}

exit

New Automation code

$credential = Get-Credential
#$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist “username”,$pass
$computernamelist = gc “C:\servers.txt”

#Copy Files
foreach ($computername in $computernamelist)

{

$Session = Enter-PSSession -ComputerName $computername -Credential $credential

$apps = Get-WmiObject -Query “SELECT * FROM Win32_Product Where Vendor LIKE ‘%ORACLE%’ AND Name like ‘%Java%’”

foreach ($app in $apps)
{
$app.uninstall()
}

exit

}

Hari,
Welcome to the forum. :wave:t4:

Before we proceed … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

What’s wrong with your code? Do you get error messages?

Regardless of that - you should reconsider using Win32_Product WMI class … ehre are some blog posts about that topic: