PowerShell is a simple, powerful and cross-platform command-line shell and scripting language designed for system administration. It helps the system administrators to manage and automate day-to-day system administration tasks. You can write your own PowerShell script to perform your tasks.
In Windows operating system, when you start any application, it will create a process for each application and assigns a unique PID. This PID is used to debug and troubleshoot system-related issues. There are a number of reasons you might want to kill a process. When you want to close the application manually and you are unable to close it. Then, you can kill the PID of that application using PowerShell. There are different methods in Windows to kill a process. Mostly everyone using the Task Manager or Windows CMS to kill the process.
In this guide, we will explain how to kill a process using PowerShell.
Open a PowerShell Interface
By default, PowerShell is installed in all modern Windows operating system.
In order to launch the PowerShell, login to the Windows machine with the Administrator user then press the Windows + R button to launch the Run windows as shown below:
Now, type powershell and click on the OK button. This will open a PowerShell window as shown below:
PowerShell allows you to run any CMD commands.
Kill a Process with TASKKILL
TASKKILL is a simple and powerful utility that can be used to kill one or more processes. It provides many options that allow you to kill your application process in an easier way.
The basic syntax of the TASKKILL command is shown below:
taskkill [option] [PID]
To list all options available with the TASKKILL command, run the following command:
You should get a list of all available options on the following screen:
A brief explanation of each option is shown below:
- /S – Specify the remote system that you want to connect.
- /U – Specify the username of the system.
- /P – Specify the password of the system.
- /PID – Specify the process id of the application that you want to kill.
- /IM – Specify the name of the application that you want to kill.
- /T – Used to kill the process with any child processes.
- /F – USed to kill the process forcefully.
How to Kill a Process
First, you will need to find out the PID of any application that you want to kill or close.
You can get a list of all running application's PIDs using the following command:
You should see the following output:
You can also filter the process list as per your requirement. For example, to get the PID of the application Chrome, run the following command:
tasklist /FI "imagename eq chrome.exe"
Now, choose the PID of any process from the above list. In this case, 1712 is the PID of cmd.exe. Now, run the following command to kill the process:
taskkil /F /PID 1712
If you want to kill the application by name, run the following command:
taskkill /F /IM cmd.exe
Kill a Process with Stop-Process
The Stop-Process is another command-line utility in PowerShell that is used to kill the process its own way. It is very similar to TASKKILL but provides different options.
Before starting, get a PID of all running processes with the following command:
You should see the following output:
Now, stop the process with ID 2200 with the following command:
stop-process -id 2200 -Force
If you want to stop the process by its name, run the following command:
stop-process -name Chrome -Force
If you want to display a prompt before stopping the process, run the following command:
stop-process -name Chrome -Force -confirm -passthru
The PassThru option will help you to keep track of process objects.
In this guide, you learned how to kill a process using the stop-process and taskkill command in PowerShell. You can also kill a process in the remote server using PowerShell. You can also use both utilities to create a PowerShell script that monitors and kill risky processes.