Table of Contents
Using Stop-Process Cmdlet
Use the Stop-Process cmdlet to kill a process by name in PowerShell.
|
1 2 3 |
Stop-Process -Name "process_name" |
The Stop-Process command stops a process by its name. The -Name parameter specifies the name of the process to stop. For example, if you want to stop the notepad program, write Stop-Process -Name "notepad".
You can check all running process by
tasklistcommand.
Use Stop-Process with Where-Object Cmdlet
Use Stop-Process with the Where-Object cmdlet to kill a process by name in PowerShell.
|
1 2 3 |
Get-Process | Where-Object {$_.Name -eq "process_name"} | Stop-Process |
First, the Get-Process command retrieves all running processes. Then the Where-Object command filters the results to include only processes with the specified name. Finally, the pipeline (|) sent the filtered results to the Stop-Process cmdlet, which stopped the process. In this method, only write the name of the process, i.e., without extension. So, for example, do not write "notepad.exe" instead, write "notepad".
Using Get-ProcessCmdlet with Process ID
Use the Get-Process cmdlet with process ID to kill a process in PowerShell.
|
1 2 3 4 |
$process = Get-Process -Name "process_name" Stop-Process -Id $process.Id |
In this solution, the Get-Process command retrieves the process object. The process object is retrieved by its name due to using the -Name parameter. The process object included information about the process, including its ID. This process object is stored in a PowerShell variable named $process.
Then, the Stop-Process command stops the process by its ID. For example, the above code will stop the instance of the "notepad" process with the lowest process ID if we replace the process_name with notepad. In this method, only write the name of the process without extension. For example, do not write "notepad.exe" instead, write "notepad".
Using Terminate() Method
Use the Terminate() method to kill the process by name in PowerShell.
|
1 2 3 4 |
$process = Get-WmiObject -Class Win32_Process -Filter "Name='process_name.extention'" $process.Terminate() |
The Get-WmiObject cmdlet is used to retrieve the process object for a process by its name. Next, we used the Win32_Process class to access process-related information in the Windows Management Instrumentation (WMI) namespace. After that, the results were filtered to include only processes with a specific name using the -Filter parameter.
The process object is then stored in a PowerShell variable named $process. Finally, the Terminate() method stops the process. Make sure to write the extension of the process; for example, if you are terminating the paint application, write mspaint.exe in the double quotes.
Further reading:
Using taskkill Command
Use the taskkill command to kill a process by name in PowerShell.
|
1 2 3 |
taskkill /F /IM "process_name.extention" |
The taskkill command stops a process by its name. The /F switch forced the process to terminate. The /IM switch specified the image name of the process to stop. Make sure to write the extension of the process; for instance, if you are terminating the paint application, write mspaint.exe in the double quotes.
If you want to check the currently running task, use the tasklist command.
|
1 2 3 |
tasklist | more |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ System Idle Process 0 Services 0 8 K System 4 Services 0 144 K Registry 160 Services 0 77,324 K smss.exe 520 Services 0 1,240 K csrss.exe 828 Services 0 6,328 K wininit.exe 908 Services 0 7,092 K csrss.exe 928 Console 1 6,896 K services.exe 980 Services 0 10,264 K lsass.exe 996 Services 0 21,228 K winlogon.exe 740 Console 1 11,960 K svchost.exe 1056 Services 0 35,724 K fontdrvhost.exe 1084 Services 0 3,816 K fontdrvhost.exe 1092 Console 1 7,748 K WUDFHost.exe 1188 Services 0 6,996 K svchost.exe 1228 Services 0 15,944 K svchost.exe 1280 Services 0 9,032 K |
That’s all about PowerShell kill process by name.