PowerShell script to find the process and kill the process
I am a beginner in PowerShell script and trying to make a small PowerShell script to find the PID by searching the filename and if shows that multiple PIDs then take the TOP one and kill that PID.
I am able to find the PID through Get-Process, but how do I store the value of the top one in variable and then kill that?
One of the nice things about PowerShell, is you usually don't need to store values. You can just pipe commands together.
Something like this should work:
Get-Process | Where-Object { $_.Name -eq "myprocess" } | Select-Object -First 1 | Stop-Process
And the breakdown is:
- Get-Process gets a list of all of the running processes
- Where-Object filters the list of processes to only those whose "Name" is equal to "myprocess"
-
Select-Object the
-First 1
selects the first entry from the list - Stop-Process stops the process passed to it