Close specific java process/application with PowerShell

In a previous article we had a look on how to close a specific java process. If you have multiple Java applications opened and start up the Task Manager, you will only see the Java.exe process running, and you cannot know which process belongs to which application. So if you want to terminate the process programmatically it might be a little bit tricky.

In the last article we learned about the Process Explorer, which I’m not going to cover in this article. In this article, we will have a look at a PowerShell script which achieves the same goal as the VBScript.

With PowerShell it’s even easier. The script is as follows:

$CommandLines = Get-CimInstance Win32_Process  

foreach ($command in $CommandLines)

{

    If ($command.CommandLine -like "*GetInputData*"){

        write-host $Command.processId

        Stop-Process -id $Command.processId

    }

}



What we do is get all Win32_Processes. Then, we parse each one and see if the command line matches our Java application name. In our example, the Java application name was demo, so you can see we search if we find something like “GetInputData”. If we find this in a command line for a specific process, we can then terminate the process by using the Stop-Process cmdline with our Process ID.

Sharing is caring!

Leave a comment

Your email address will not be published. Required fields are marked *

1 × five =