Powershell

How to kill a process using PowerShell

PowerShell is a Windows software used for configuration management and it also performs administrative tasks. The major responsibility of a system administrator is to keep the system running without any hindrance. To do so, the system administrators tend to kill a few processes that are either not functioning well or disturbing the other processes. This post aims to explore the possible methods to kill processes using PowerShell. The possible outcomes of this post are:

  • How to kill a process using TASKKILL in PowerShell?
  • How to kill a process using Stop-Process in PowerShell?

Method 1: How to kill a process using TASKKILL in PowerShell?

TASKKILL is an administrator command which is used by PowerShell and CMD to kill any running process. TASKKILL uses PID (Process Identification) or the ProcessName to identify the process number and then kills it.

Syntax: To kill a Process by ID

> TASKKILL /F /PID

Syntax elements are explained below.

  • /F: Forcely kill the process
  • PID: Specifies the process ID which is about to be terminated

Syntax: To kill a process by Name

> TASKKILL /IM  /F

The elements of syntax are:

  • TASKKILL: used to kill a running task
  • /IM: used to specify the image name of the process to be terminated
  • /F: Forcely kill the process

Example
Here, we have presented the usage of the TASKKILL command which is basically used to kill a process.

If you want to get the process image name or its ID using PowerShell, you can get it via the following command:

> TASKLIST

In the above output, the first column “Image Name” represents the images present, and the second “PID” represents the process IDs, the third column “Session Name” represents services names, fourth column “Session#” tells whether the session is on or off by using 1 and 0.

Select the task you want to kill and write down its id in the command below to kill it:

> TASKKILL /F /PID 5276

From the “SUCCESS” message, it is observed that the process has been killed successfully.

TASKKILL also allows the killing of a task by specifying its name.

Specify the Name of the process you want to kill after the /IM parameter as follows:

> TASKKILL /IM calc.exe /F

Congratulations! You have learned to use TASKKILL to kill a process by its ID as well as by its name.

Method 2: How to Kill a process using Stop-Process in PowerShell?

The Stop-Process cmdlet is an administrative command that is used to kill the running processes. The Stop-Process command uses PID and the name to identify the process while killing.

Syntax: Kill the process by ID

> STOP-PROCESS -ID <Process-Id> -Force

Syntax elements are described as

  • ID: species the ID syntax
  • Force: forces the process to implement

Or

Syntax: To kill the process by name

> STOP-PROCESS -Name <Process-Name> -Force

Syntax elements are explained as

  • Name: specifies the name syntax
  • Force: forces the process to implement

Example
This example demonstrates the working of the TASKKILL command.

First of all, run the command of TASKLIST to get the list of tasks.

> TASKLIST

Specify the process that you want to kill and then enter its ID

> STOP-PROCESS -id 5704 -Force

Hence, the process is terminated by using the Stop-Process cmdlet.

STOP-PROCESS also allows you to kill the process by specifying its name.

> STOP-PROCESS -Name notepad -Force

The process named “notepad” is killed successfully.

Congrats! You have learned successfully to kill the process using PowerShell.

Conclusion

To kill a process, PowerShell supports Stop-Process and TASKKILL commands. Both methods trace the process either by using the process name or id. This post has provided a list of possible methods to kill a process using PowerShell. You can also get the list of the processes via the Get-Process or TASKLIST commands in PowerShell.

About the author

Adnan Shabbir