Search code examples
javapowershellwmicstart-process

PowerShell terminate java process detected by its commandline


I am starting a java process:

$global:TS = Start-Process -FilePath "$pathTS\jdk\bin\java.exe" -ArgumentList "-jar","`"$pathTS\TestServer.jar`"" -WorkingDirectory ` "$pathTS" ` -PassThru -WindowStyle Hidden

works fine so far.

Now I am trying to terminate that process:

 Invoke-CimMethod -Query 'select * from Win32_Process where CommandLine like "Test-Server"' -MethodName "Terminate"

BTW: I cannot use the ID of the process to terminate it since it is changing somehow.

I am new to PS and now stuck. Maybe someone has in idea.

Thank you! Alex


Solution

  • Try to add the wildcard character % to the WHERE clause to find all instances where "Test-Server" is found anywhere in the CommandLine :

    ... WHERE CommandLine like "%Test-Server%"...
    

    ref : https://learn.microsoft.com/en-us/windows/win32/wmisdk/like-operator