PowerShell Kill Process by Name

PowerShell kill process by name

Using Stop-Process Cmdlet

Use the Stop-Process cmdlet to kill a process by name in PowerShell.

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 tasklist command.

Use Stop-Process with Where-Object Cmdlet

Use Stop-Process with the Where-Object cmdlet to kill a process by name in PowerShell.

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.

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.

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.

Using taskkill Command

Use the taskkill command to kill a process by name in PowerShell.

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.

That’s all about PowerShell kill process by name.

Was this post helpful?

Leave a Reply

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