DevOps

PowerShell Kill a Process from the Command Line

Learn how to use PowerShell kill a process in Windows cmdlets. Compare PowerShell with the command prompt, scripts, and advanced techniques.

Let’s see how we can use the command line, including command prompt commands and PowerShell to kill processes.

Task Manager may not be the best tool

The classic Windows Task Manager is the go-to for many when it comes to process management. However, launching Task Manager might not always be the best solution, especially in cases where processes need to be ended on remote computers or the tool won’t launch for whatever reason.

Task manager has long been the tool in Windows to kill processes.

Using the windows task manager to view and manage processes
Using the windows task manager to view and manage processes

For that, we need to use the command line, including the classic command prompt, and we can also use PowerShell to kill a process.

Tasklist and Taskkill

Let’s first look at how to use the traditional command line to find and kill processes using tasklist and taskkill.

Each running process in a Windows environment is assigned a unique process id (PID). This identification and the image name make it easier for users to single out specific processes. The process name and the PID are essential when deciding which process to terminate.

Before killing a process using taskkill, you often use tasklist to identify the running processes.

Running the tasklist command
Running the tasklist command
tasklist

You can use the “/?” parameter to view the command help.

Viewing tasklist help
Viewing tasklist help

Killing a Process Using its Name (Image Name)

The /IM switch allows you to specify the image name (essentially the process name) of the process you want to terminate.

taskkill /IM notepad.exe /F

Killing a Process Using its PID

Using the /PID switch, you can target a specific process using its Process ID.

taskkill /PID 1234 /F

Killing Processes Based on Session Name or Session Number

This is particularly useful in a terminal server environment.

taskkill /FI "SESSION eq 1" /F

Killing Processes Based on Status

For example, you might want to terminate all not responding processes.

taskkill /FI "STATUS eq NOT RESPONDING" /F

Killing Processes on Remote Machines

If you have the necessary permissions, taskkill also allows you to terminate processes on remote systems.

taskkill /S RemotePC1 /U username /P password /IM notepad.exe /FREM Kill all instances of Notepad on a remote computer named 'RemotePC1' 

Killing Multiple Processes Simultaneously

You can specify multiple image names separated by space or comma to stop one or more processes found in the process objects:

taskkill /IM notepad.exe,calc.exe /F

PowerShell kill a process

PowerShell is a new scripting language from Microsoft that provides a noun-verb construct for entering commands that is very intuitive and powerful. It allows easy managing Windows environments and can easily be used for automation scripts that can automate repetitive tasks.

Aside from automation, it enables interacting with Windows, including managing running processes and much more. Using PowerShell’s verb-noun construct, we can take the process object and end the specified process using PowerShell’s object-oriented language.

Identifying Running Processes with PowerShell and Killing them

The first step in managing a process is process identification. With the get process command, one can easily list all running processes. The tasklist command can also be employed from the command prompt to achieve similar results.

Before you can kill a process, you often need to identify it. Open a PowerShell Window and type the “Get-Process” cmdlet.

# List all running processes 
Get-Process
Running the get process cmdlet
Running the get process cmdlet

Filtering Processes by Name

You can filter by name if you’re interested in a specific process or a set of processes.

# List all instances of Notepad 
Get-Process -Name notepad
Getting processes by name for powershell kill a process purposes
Getting processes by name for powershell kill a process purposes

Filtering Processes by Process ID (PID)

Each running process has a unique Process ID (PID). This allows for precise identification.

# List process with a specific PID (e.g., 1234) 
Get-Process -Id 1234
Getting a process by id
Getting a process by id

PowerShellโ€™s Stop Process Cmdlet

An alternative to the taskkill command is the stop process command in PowerShell. Combined with the where object cmdlet, this cmdlet lets users filter out processes by various criteria and then terminate them. You can kill it using its name once you’ve identified a rogue process.

# Kill all instances of Notepad 
Stop-Process -Name notepad -Force

Killing a Process Using its PID

For a more targeted approach, especially if multiple instances of a process are running, use the PID.

# Kill process with PID 1234 
Stop-Process -Id 1234 -Force
Stop a process with the force parameter
Stop a process with the force parameter

Advanced Filtering Using Where-Object Cmdlet

You might want to kill processes based on specific conditions. The Where-Object cmdlet is perfect for this.

# Kill all processes consuming more than 1GB of memory 
Get-Process | Where-Object {$_.WS -gt 1GB} | Stop-Process -Force

Below, I queried first for 1GB and there were no processes taking that amount of memory. Next, I tried 512MB and again nothing. Then I lowered down to 100MB and you can see the processes returned.

Querying processes that are using a certain amount of memory
Querying processes that are using a certain amount of memory

Killing Processes on Remote Machines

Sometimes, processes may be running on remote servers or machines. With Windows Management Instrumentation capabilities, Windows PowerShell allows users to manage processes on their local computers and remote systems.

# Kill all instances of Notepad on a remote computer named 'RemotePC1' 
Invoke-Command -ComputerName RemotePC1 -ScriptBlock { Stop-Process -Name notepad -Force }

Batch Termination of Processes

If you want to terminate multiple processes at the same time, you could use the following as an example:

# Kill all instances of Notepad and Calculator 
Get-Process | Where-Object { $_.Name -in "notepad", "calc" } | Stop-Process -Force

The Force Switch and Risky Processes

While most processes can be halted gracefully, some may resist standard termination methods. You can use the force switch to end processes forcefully. However, be careful doing this, as it might lead to data loss or instability.

# Forcefully kill all instances of Notepad
Stop-Process -Name notepad -Force

Wrapping up

As we’ve seen, PowerShell provides an excellent set of tools for handling all types of processes, from a simple notepad process to more complex Windows processes. The command line and using PowerShell are the best ways to manage and stop processes at scale using automation.

As shown, you can even use PowerShell to kill a process remotely. The next time you have a hung process, use PowerShell instead of the Task Manager, and you will be surprised at the ease with which you can stop processes from the PowerShell terminal.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

One Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.