This tutorial will guide you through the process of identifying and terminating processes using PowerShell. You will learn how to open and navigate the PowerShell environment, get a list of running processes, and use commands like ‘Taskkill’ and ‘Stop-Process’ to terminate processes by their ID or name. We will also briefly discuss how to kill a process without PowerShell using the Task Manager.
Networking and Cyber Security Specialist
Updated: November 30, 2023
Related post: 25 Essential PowerShell Commands
Windows assigns a Process ID (PID) to each process that it starts up. So you need to identify the PID for the task you want to terminate.
The steps are:
Open the PowerShell environment
2. Windows will ask you for your permission to proceed. Click OK and the PowerShell app will open. This shows a blue background and has the PowerShell prompt at the top of it. The prompt also shows the current directory which defaults to C:\Windows\system32.
3. If you want to run your own scripts from this prompt, you can change the directory with the command cd <directory>
(substitute the directory name you want to move to for <directory>).
Get a list of running processes
All the methods available to kill a process require a PID as a parameter. The list of running processes can be long but you can move up and down the screen by using the slider bar to the right of the PowerShell Window.
The PID is the second column in the output. The first column lists the names of the processes. You will notice that a lot of the processes are called svchost.exe. This is not very helpful because if you want to stop one of these processes, it is impossible to work out which is the one that is giving you trouble.
Once you have identified the process you want to terminate, you have two options to kill it: taskkill and stop-process.
Note that all methods to kill a process require a PID
Kill a process with Taskkill
Taskkill allows you to kill a process either by its PID or by the name listed for it in the tasklist output.
To stop a process by its ID, use taskkill /F /PID <PID>
, such as taskkill /F /ID 312
7 if 3127 is the PID of the process that you want to kill.
To stop a process by its name, use taskkill /IM <process-name> /F
, for example taskkill /IM mspaint.exe /F
.
Kill a process with Stop-Process
Like Taskkill, Stop-Process lets you use either the PID or process name to kill a process. The name needs to be as shown in the tasklist output.
To stop a process by its ID, use the format: Stop-Process -ID <PID> -Force
, eg. Stop-Process -ID 3127 -Force
.
To stop a process by its name, use the format: Stop-Process -Name <process-name> -Force
, eg. Stop-Process -Name mspaint.exe -Force
.
Kill a process without PowerShell
If you just want to kill a process and you aren’t interested in using a command that you can put in a script, the easiest method is through the Task Manager, which is part of Windows.
- To get Task Manager, right-click on a vacant space on the taskbar and select Task Manager from the context menu.
- In Task Manager, scroll through the list of running processes that are shown in the Process tab of the interface.
- Click on the process that you want to stop and then click on the End task button at the bottom-right of the interface.
Using Software
SolarWinds Server & Application Monitor
Take a look at the SolarWinds Server & Application Monitor. This tool includes process management. This has a screen that shows all running processes and also includes a kill button. This utility can be set to look for specific conditions, such as a process that runs longer than a given time or one that seems to be inactive. Under these circumstances, you can set the system to send you an alert, so you don’t have to sit looking at the screen all day in order to keep track of problematic processes. SolarWinds offers the Server & Application Monitor on a 30-day free trial.
SolarWinds Server & Application Monitor
Get a 30-day FREE Trial
How do I stop a PowerShell command from running?
You can interrupt and stop a PowerShell command while it is running by pressing Control-C. A script can be stopped with the command exit. This will also close the PowerShell console.
How do I kill Windows processes from the command line?
At the command line, you can terminate a Windows process with the command taskkill. To use this command, you need to know its process ID (PID). You can get a list of all running tasks with the command tasklist. Once you know the PID, use the taskkill command in this manner: taskkill /PID <PID> /F. Type in the process ID without quotes instead of <PID>.
PowerShell offers a way to manage processes programmatically with scripts. However, it can be time-consuming, and numerous pre-existing tools may perform process management more efficiently than a simple script you create.
3 min read
Now you can’t run your application because another process already uses the port. How can you find that process? How to kill it?
Table of Contents
Just a second!
If you are here, it means that you are a software developer.
So, you know that storage, networking, and domain management have a cost .Thank you for your understanding.
– Davide
Sometimes, when trying to run your ASP.NET application, there’s something stopping you.
Have you ever found a message like this?
Failed to bind to address https://127.0.0.1:7261: address already in use.
You can try over and over again, you can also restart the application, but the port still appears to be used by another process.
How can you find the process that is running on a local port? How can you kill it to free up the port and, eventually, be able to run your application?
How to find the process running on a port on Windows 11 using PowerShell
Let’s see how to identify the process that is running on port 7261.
Open a PowerShell and run the netstat
command:
NETSTAT is a command that shows info about the active TCP/IP network connections. It accepts several options. In this case, we will use:
-n
: Displays addresses and port numbers in numerical form.-o
: Displays the owning process ID associated with each connection.-a
: Displays all connections and listening ports;-p
: Filter for a specific protocol (TCP or UDP)
Notice that the last column lists the PID (Process ID) bound to each connection.
From here, we can use the findstr
command to get only the rows with a specific string (the searched port number).
Now, by looking at the last column, we can identify the Process ID: 19160.
How to kill a process given its PID on Windows or PowerShell
Now that we have the Process ID (PID), we can open the Task Manager, paste the PID value in the topmost textbox, and find the related application.
In our case, it was an instance of Visual Studio running an API application. We can now kill the process by hitting End Task.
If you prefer working with PowerShell, you can find the details of the related process by using the Get-Process
command:
Then, you can use the taskkill
command by specifying the PID, using the /PID
flag, and adding the /F
flag to force the killing of the process.
We have killed the process related to the running application. Visual Studio is still working, of course.
Further readings
Hey, what are these fancy colours on the PowerShell?
It’s a customization I added to show the current folder and the info about the associated GIT repository. It’s incredibly useful while developing and navigating the file system with PowerShell.
🔗 OhMyPosh and CascadiaCode not working on PowerShell? How to fix it in Windows 10 and Windows 11 Integrated Terminal
This article first appeared on Code4IT 🐧
Wrapping up
As you can imagine, this article exists because I often forget how to find the process that stops my development.
Have you ever encountered a program that has frozen or become unresponsive on your Windows computer? It can be frustrating to deal with, especially if you need to close the program quickly. Fortunately, PowerShell provides a simple solution to end a process without having to go through the Task Manager.
Key Takeaways:
- Killing processes in PowerShell are essential for effective system management and troubleshooting.
- PowerShell offers a Stop-Process cmdlet to terminate processes by their ID or Name.
- Use the
Get-Process
cmdlet to find the process ID (PID) of a specific process. - If a process does not terminate with a standard
Stop-Process
command, using the-Force
parameter might be necessary.
Table of contents
- Understanding the need to kill a process in PowerShell
- How to Kill a Process in PowerShell?
- PowerShell script to kill a process if running
- Kill Multiple Processes at Once
- Kill Remote Processes
- Alternate Ways to Kill a Process
- Conclusion
Understanding the need to kill a process in PowerShell
Terminating processes can be crucial for various reasons: to free up system resources, to end non-responsive applications, or to stop a process running beyond its required lifecycle. This is where PowerShell steps in, offering a powerful and efficient way to control processes.
What is Processes in Windows?
Before we begin, let’s understand what a process is. In layman’s terms, a process is a running instance of a program. Each process in a Windows environment has a Process ID (PID) associated with it, a unique identifier that allows you to manage it effectively.
Scenarios Requiring Process Termination
There are several reasons why a process may need to be killed, including unresponsiveness, high resource usage, or conflicts with other processes or applications. Killing a process can help to free up system resources and prevent further problems.
- Unresponsive Applications: When an application becomes unresponsive or freezes, terminating its associated process can help regain control and restore system stability.
- Resource Management: Sometimes, certain processes may consume a large amount of system resources, leading to slow performance. By terminating these processes, you can free up system resources and enhance overall performance.
- Error Handling: If a process encounters an error or behaves unexpectedly, terminating it can help resolve the issue and prevent further complications.
- Causing problems for the system or other applications
Step 1: Identify the Process to Terminate
To terminate a process, we first need to identify it. PowerShell provides a few different ways to do this, primarily through the Get-Process
cmdlet. Running Get-Process
without any parameters will list all running processes on your machine.
This will provide you with a list of processes along with their properties like Handles
, NPM(K)
, PM(K)
, WS(K)
, VM(M)
, CPU(s)
, Id
, SI
, ProcessName
etc. The Id
is the PID which uniquely identifies the process.
To find a specific process, you can filter by the process name by the below syntax:
Get-Process -Name <ProcessName>
Replace ProcessName
with the name of the process you’re interested in.
Get-Process -Name "notepad"
This will return a list of all the “notepad” processes running on your computer, including their process ID (PID) and CPU and memory usage. Once you have identified a process, you can terminate it using the Stop-Process command.
If you want to find processes by other properties like ID, you can do so as well:
Get-Process -Id <ProcessId>
Replace the ID parameter ProcessId
with the specific ID of the process. Once you have identified the process, you can terminate it using the Stop-Process
command.
Step 2: Kill the Process using Stop-Process Cmdlet
- Open PowerShell by pressing
WIN+X
and select “Windows PowerShell (Admin)” - Type
Get-Process
to view a list of running processes and their names - Identify the name of the process that you want to kill
- Type
Stop-Process -Name <ProcessName>
to kill the process
Stop-Process -Name "notepad"
This will kill all the “notepad” processes running on your computer.
By default, the stop-process doesn’t prompt for confirmation before stopping the process. You can use the -Confirm
switch to get the confirmation:
Get-Process -Name "Notepad" | Stop-Process -Confirm
The above script gets the process “Notepad” and passes the process object to the pipeline to stop-process.
Using the Process ID to Kill the Process
- Open PowerShell by pressing
WIN+X
and select “Windows PowerShell (Admin)” - Type the PowerShell command
Get-Process
to view a list of running processes and their process IDs - Identify the process ID of the process that you want to kill
- Type
Stop-Process -Id <ProcessID>
to kill the process
Replace with the process ID you intend to terminate. For example, let’s kill the process with process identifier 100944:
This command is useful when you have multiple processes with the same name running but want to stop only one.
Forcefully Kill Processes
Stop-Process -Name "ProcessName" -Force
This will kill the process forcefully without giving it a chance to terminate gracefully. However, this can be used as a last resort, as forcefully killing a process can cause data loss or corruption.
Step 3: Confirm the Process is Ended
After you have entered the command, PowerShell will end the process immediately. To confirm that the process has ended, you can type the Get-Process command again to see if the process is still running. If it is not listed, then the process has been successfully ended.
After killing the process, you’d want to ensure that the process has indeed stopped. You can use the Get-Process
cmdlet to verify this:
Get-Process -Id ProcessId -ErrorAction SilentlyContinue
Replace ProcessId
with the ID of the process you just terminated. If the process is still running, it will be listed. If not, it won’t return anything.
PowerShell script to kill a process if running
In some cases, it may be necessary to automate the process of killing a process in PowerShell. This can be done using a PowerShell script that checks if the process is running and kills it if it is. Here is an example PowerShell script that kills a process if it is running:
$processName = "notepad" $process = Get-Process -Name $processName -ErrorAction SilentlyContinue if($process) { # Kill process Stop-Process -Id $process.Id }
This script checks if the process named “notepad” is running. First, we’ll assign the process name to a variable $ProcessName. Next, we use the Get-Process command to check if the process is running and assign it to the $Process variable. We use the -ErrorAction parameter to SilentlyContinue if the process isn’t running. Next, we use an if statement to stop the process if it is running. Finally, we kill the process using the Stop-process cmdlet.
How do I kill a process owned by a specific user using PowerShell?
Get-Process -IncludeUserName | Where-Object {$_.UserName -eq "Username"} | Stop-Process
Kill Multiple Processes at Once
If you want to kill multiple processes at once, you can use the Where-Object command to filter the processes that you want to kill. For example, if you would like to kill all the “notepad” processes running on your computer, you can use the name parameter:
Stop-Process -Name "notepad"
This command will return all the “notepad” processes and pipe them to the Stop-Process command to kill them all simultaneously. You can also use the below command to kill multiple processes:
Get-Process | Where-Object {$_.Name -eq "notepad"} | Stop-Process
This kills all instances of the notepad process. You can stop multiple processes by their name as:
Get-Process Notepad, mspaint -ErrorAction SilentlyContinue | Stop-Process -verbose
Kill Remote Processes
Invoke-Command -ComputerName "RemoteComputerName" -ScriptBlock { Stop-Process -Name "chrome" }
Alternate Ways to Kill a Process
Apart from the PowerShell Stop-Process cmdlet, there are alternative methods, such as the TASKKILL command Prompt and Task Manager, to terminate unwanted or problematic processes in your Windows environment.
Methods | Description |
---|---|
TASKKILL Command line tool | Allows termination of one or more processes using command prompt. |
Task Manager | A built-in Windows utility that offers process monitoring and management capabilities. |
Using the Task Manager to Kill a Process
- Press Ctrl + Shift + Esc to open Task Manager.
- Once Task Manager opens, you will see a list of running processes under the “Processes” tab.
- To sort the processes alphabetically, click on the “Name” column header.
- Locate the process you want to kill, right-click on it, and select “End Task.”
- If the process is unresponsive and not terminating, you can select “End Process Tree” instead. This will end the process and any associated child processes.
- Once the process is successfully terminated, it will no longer appear in the Task Manager’s list.
Killing a Process via TASKKILL in Command Prompt
The TASKKILL command is a powerful tool for killing processes at the command prompt. With its extensive functionality, you can terminate one or more processes efficiently and effectively. TASKKILL allows you to easily manage your processes, giving you greater control over your system.
- To begin, open Command Prompt as an administrator. Right-click on the Start button, then select “Command Prompt (Admin)” from the menu. This will launch Command Prompt with elevated permissions, allowing you to perform administrative tasks.
- Once Command Prompt is open, you can list all the running processes on your system by using the command
tasklist
. This will display a list of processes along with their process IDs (PID) and other relevant information. - Next, identify the process that you want to kill and note down the PID of the process. To terminate the process, use the command
taskkill /PID [PID]
, replacing [PID] with the actual process ID. For example, if the process ID is 1234, the command would betaskkill /PID 1234
. This will forcefully terminate the process.
Alternatively, you can use the “-IM” parameter to specify the name of the process you wish to terminate. In this case, TaskKill just kills one instance of Notepad.
Remember, killing a process via Command Prompt should be done with caution, as terminating certain system processes or critical applications can have unintended consequences. Ensure that you have the necessary knowledge and understanding of the processes you are terminating.
Conclusion
Why would I need to kill a process using PowerShell?
There are scenarios where terminating a process becomes necessary, such as when a program is unresponsive or consuming excessive resources. PowerShell provides convenient methods to manage and terminate processes efficiently.
How do I kill a process using PowerShell?
To kill a process using PowerShell, you can use the Stop-Process
cmdlet. For example: Stop-Process -Name "ProcessName"
or Stop-Process -Id ProcessID
. This will terminate the process with the specified name or process ID.
What is the TASKKILL command in PowerShell?
TASKKILL is a powerful utility in PowerShell that allows you to terminate one or more processes. It provides options for forceful or graceful termination.
How do I find the process ID (PID) of a specific process in PowerShell?
To find the process ID (PID) of a specific process, you can use the Get-Process
cmdlet. For example: Get-Process -Name "ProcessName"
. This will retrieve the process object(s) with the specified name, and you can access the process ID using the Id
property, like (Get-Process -Name "ProcessName").Id
.
What is the Stop-Process command in PowerShell?
Stop-Process is another command in PowerShell that can be used to kill a process. It provides functionality similar to TASKKILL but with some differences. We explain how to use Stop-Process and compare it with TASKKILL in our guide.
Can I kill a process using the Task Manager in Windows?
Yes, the Task Manager is a built-in Windows utility that allows you to monitor and manage running processes. It also lets you kill a process!
How can I kill a process using Command Prompt in Windows?
What happens if I try to kill a system process?
Killing a system process may cause system instability or lead to a system crash. PowerShell may also prevent you from terminating critical system processes.
How do I force kill a process that is not responding?
To forcefully kill a process using PowerShell, you can use the -Force
parameter with the Stop-Process
cmdlet. For example: Stop-Process -Name "ProcessName" -Force
. This will immediately terminate the process without giving it a chance to clean up or save its state.
How do I view all the processes currently running on my system with PowerShell?
Use the Get-Process
cmdlet without any parameters to list all the processes running on your system.
Introduction
When writing a script or simply working from the command line, sometimes it is necessary to kill a process.
In Windows, this can be done in several ways depending on the terminal tool you are using (cmd, powershell, and so on).
In this post, I will show you how to kill a process from the command line in Windows.
How to kill a process from cmd
To kill a process from the command line, we will use the taskkill
command.
This command is available in Windows XP and later versions of Windows.
taskkill /F /IM <process name>
How to kill a process from PowerShell
To kill a process from PowerShell, we will use the Stop-Process
cmdlet.
Conclusion
In this post, I’ve shown you the possible ways to kill a process from the command line in Windows.
Using the taskkill
command from cmd or the Stop-Process
cmdlet from powershell.
I hope you find it useful.
Handling programs that become unresponsive or consume excessive CPU or memory resources can enhance your computer’s performance or resolve issues they cause. If you have problems with Task Manager, the taskkill command in Command Prompt lets you end one or more processes to gain control over your computer’s resources and keep it running smoothly.
Why you need to know the Windows kill process
Ending Windows processes can fix some system performance issues, like unresponsive programs or those consuming excessive system resources. Terminating processes can free up memory and CPU resources to restore system stability and speed.
However, ending critical system processes or important applications abruptly can result in system instability or data loss. For instance, terminating a process that is writing data to disk might corrupt the file being processed — or worse, cause system errors.
Therefore, while it’s beneficial to know how to kill a process in Windows using tools like Task Manager, it’s equally important to understand which processes are safe to end and which might have critical consequences if stopped prematurely, to ensure you avoid unnecessary problems.
Resolve Windows endpoint issues silently with NinjaOne’s background Task Manager and remote management tools for Windows.
Understanding process IDs (PIDs)
A PID is a unique decimal number assigned to each process running in Windows. This identifier is crucial when you need to specify a process, for instance, when attaching a debugger or managing system resources. PIDs ensure that each process can be individually addressed, even if multiple instances of the same program are running.
You can find a PID in Windows using one of these ways:
- Task Manager: Access Task Manager and click on the “Details”’ tab to view the PID for each process.
- Command Prompt: Open Command Prompt and run the “tasklist” command to display the PIDs for all running processes.
- PowerShell: Run the “Get-Process” command in PowerShell to specify a process and display its PID.
Whether you need to end Windows processes to troubleshoot an issue, manage resources or because you want to install another application, it’s a good idea to know different ways of doing it.
1. Run the Windows kill process with Task Manager
- Open Task Manager by clicking Ctrl + Alt + Delete then selecting “Task Manager” from the options. Another option is to press Windows + X and choose “Task Manager” from the menu.
- Once open, ensure you are on the “Processes” tab to view the list of all running processes.
- To end a process, select it and click on the “End Task” button at the bottom right of the window. Alternatively, you can right-click on the process and select “End Task” from the context menu.
If the process is unresponsive, opening Task Manager and using the “End Task” feature will forcibly close the program, freeing up system resources. Before doing this, make sure the process you are ending is safe to stop and ending it will not cause system instability.
2. Run the Windows kill process using Command Prompt with “taskkill”
Kill tasks or processes using the taskkill command in Windows with these steps:
- Open Command Prompt by clicking the Start button and typing “Command Prompt” or by typing “cmd” in the search box. Select “Run as administrator.”
- Type “tasklist” to display all currently running processes along with their PIDs.
- If you have a process’s PID, kill the task with the command: “taskkill /F /PID pid_number” replacing pid_number with the actual PID of the process you want to terminate.
- To end a process by its name, use the command: “taskkill /IM process name /F” replacing “process name” with the name of the executable file of the process.
This command can end a process if Task Manager doesn’t end it and you don’t want to restart your device.
3. Run the Windows kill process with PowerShell
Here’s how to end Windows tasks using the PowerShell Stop-Process cmdlet:
- Open an elevated PowerShell by right-clicking on the Start menu, selecting “Windows PowerShell (Admin)” and confirming any user control prompts.
- Type “Get-Process” to display a list of all currently running processes, their names and PIDs.
- If you know the name of the process, you can terminate it with the PowerShell Stop-Process by typing Stop-Process -Name “ProcessName” -Force. Replace “ProcessName” with the actual name of the process.
- If you have the process ID, type Stop-Process -ID PID -Force, substituting PID with the actual numerical ID of the process.
- To stop all instances of a specific process, such as when multiple windows of the same application are open, use Stop-Process -Name “ProcessName” -Force.
Remember that inadvertently closing essential system processes can make your system unstable or crash it.
4. Create desktop shortcuts for killing processes in Windows
You can create desktop shortcuts to run the Windows kill process directly from your desktop:
- Right-click on an empty area of your desktop, hover over “New” and then select “Shortcut” from the context menu.
- In the shortcut creation wizard, type the command taskkill /f /fi “status eq not responding” into the location field.
- Click “Next” and name your shortcut. Choose a name that clearly indicates the shortcut’s function, such as “Kill Not Responding Processes.”
- Click “Finish” to create the shortcut on your desktop.
- Once the desktop shortcut is created, right-click on the shortcut and select “Properties.”
- In the Shortcut tab, click in the “Shortcut key” field and press the key combination you wish to use for this shortcut. Windows will automatically prepend “Ctrl + Alt,” making the full shortcut. For example, if you press “K,” the full shortcut will become “Ctrl + Alt + K.”
- Click “OK” to save your settings.
These steps let you quickly set up a desktop shortcut that allows you to terminate unresponsive processes with just a double-click or a keyboard shortcut.
Save time and automate killing unnecessary processes on Windows systems with NinjaOne automation.
Download the guide here.
How to kill a process in Windows with third-party tools
Third-party tools allow you to manage and terminate processes on your Windows system while providing detailed insights and extended functionalities. Some third-party applications to consider include:
Process Explorer: A free Microsoft program, Process Explorer provides a detailed view of the files or directories opened by different programs.
Process Hacker: This free tool offers a color-coded tree view of processes, manages services and network connections and provides real-time diagnostics.
MiTeC Task Manager DeLuxe: Task Manager DeLuxe is a portable application that has the features of Task Manager with additional enhancements and a process monitor that lets you view multiple processes simultaneously.
System Explorer: This open source portable executable acts as a Task Manager replacement and gives you a detailed view of tasks, processes, modules and more in Windows.
AnVir Task Manager: This free program provides comprehensive details on system resources and includes spyware removal capabilities.
Daphne Portable: This is a free system-tray application to control, debug and kill Windows processes.
Moo0 SystemMonitor: Offering real-time system resource monitoring, this freeware tool lets you keep an eye on the health and performance of your PC.
Managing Windows System Resources
Between Command Prompt and the Windows kill process, Task Manager and third-party tools, you have several options for managing processes. Whether you need to stop a single process or detailed diagnostic information or enhanced control over running processes, these applications can offer the necessary functionality to optimize your system’s performance.
Managing processes in complex Windows environments can be an overwhelming and time-consuming experience. Having to be constantly logging into different machines at different times, hitting the “Ctrl+Alt+Del”, looking for specific processes, and “Ending Tasks” is a long and tedious process.
With PowerShell (PS), you can programmatically find and stop a service. You can use PS’s cmdlets to create scripts to look for and stop a specific service, scripts that kill a process based on specific criteria, auto-startup scripts, or whatever sets your imagination.
In this tutorial, you’ll learn how to locate and kill any process using two different commands available in PowerShell, the TASKKILL and the Stop-Process cmdlet. The advantage of using PowerShell over simple CMD, is that you can create automation routines to close processes.
Table of Contents
- Kill a process using PowerShell
- Why would you want to kill a process?
- Install and Open PowerShell.
- Open the PowerShell Interface.
- Killing a process with TASKKILL
- How to use TASKKILL?
- Forceful (/f) vs Graceful?
- Task Listing.
- Using TASKKILL in PowerShell?
- Killing a Process with PowerShell’s Stop-Process
- TASKKILL vs Stop-Process?
- A Stop-Process Example?
- Conclusion
Kill a Process Using PowerShell
When an application (or service) starts, Windows OS creates a process for the executable file. This process contains the code and the current activity. Additionally, the OS also creates a unique Process Identifier (PID) for that particular process. This PID is a decimal number that can be used for debugging or troubleshooting.
An example is when you open an application such as Chrome or Skype, Windows creates a particular PID for each of those applications. You can use this PID to attach a debugger, monitor it, or kill the process.
Why would you want to kill a process?
The two traditional ways to kill a process are via the Windows Task Manager and the CMD command prompt. The third way, not so common but very efficient, is using PowerShell.
Install and Open PowerShell
PowerShell (PS) is Microsoft’s automation and configuration management framework. It comes with its own command-line shell and scripting language. PS works well with any tool and is optimized to work with structured data (CSV, XML, JSON, etc), and REST APIs.
Microsoft’s PS is open-source and available as a cross-platform. It is available on Windows, Linux, or macOS.
To download, install, or update the latest stable version of PowerShell, visit the GitHub repository: https://github.com/PowerShell/PowerShell/
Now, let’s open the PowerShell Interface.
Press “Windows + R” keys to open the run box, and type “PowerShell”. Clicking “Ok” will open a regular PS interface. But you can also open an interface with elevated permissions, by pressing Ctrl+Shift+Enter.
Although the PS interface looks a lot like Windows Command Prompt (cmd), PS is a more advanced “version” of cmd. As mentioned before PS comes with its own scripting language and command-line shell.
With PowerShell, you can run any cmd command, like ListTask or KillTask.
Killing a process with TASKKILL
Let’s start by defining the TASKKILL utility (taskkill.exe) and how to use it.
TASKKILL is a Microsoft utility that allows you to terminate one or more processes (or tasks). At the basic level, TASKKILL is like clicking the X button on the top-right corner of any Windows application. It will “gracefully” exit the program and will prompt you “whether to save changes” before exiting. However, the TASKKILL utility gives you more flexibility on how you would want to kill a process— you can go gracefully or forcefully.
This utility can be used from the command line with different configuration arguments such as /F, /T, PID, and /IM.
The TASKKILL command syntax is (As shown in the screenshot above):
The useful parameters to kill a process with TASKKILL are:
- /S (System) Define the remote system to connect to.
- /U (Username) Specify the username.
- /P (password) Determines the password for that specific user.
- /PID (Process ID) Specify the ID of the process you want to terminate.
- /IM (Image name) Specify the image name of the process you want to terminate.
- /T (Terminate) Terminate the PID along with any child processes associated with it.
- /F (Forceful) Forcefully terminate the process.
How to use TASKKILL?
Let’s put together a couple of TASKKILL parameters.
taskkill /PID process-number /F
taskkill /IM process-name /F
Forceful (/f) vs Graceful?
As many of us have probably experienced before, the graceful way to exit a program (the “X” on top of a Windows bar) will usually not work, if an application is frozen or buggy.
When you force a process to exit (forceful kill), you are doing the same as Alt+F4 or going to the task manager with Ctrl+Alt+Del and clicking on “End Task”. Without the parameter (/F) is like clicking on the (X) on the top bar of any window.
So, you’ll need to force a program to exit with (/f) in some cases.
Task Listing
You can also filter your search based on some criteria using the “/fi” parameter. For example:
tasklist /fi "imagename eq notepad.exe"
Again, if you want to know more about the Tasklist command, type “tasklist/?” on the command prompt.
Using TASKKILL in PowerShell?
First, as mentioned before, use the TASKLIST command to find the Image Name and its PID:
Find the name of the process and record the PID or image name (i.e., notepad.exe).
In this example, “notepad.exe” is using the PID:13252
- To gracefully kill the notepad process with pid:
taskkill /pid 13252
- To forcefully kill the notepad process with pid:
taskkill /pid 13252 /f
- To forcefully kill the notepad process using image name:
taskkill /im notepad.exe /f
Kill a Process with PowerShell’s Stop-Process
The Stop-Process is PowerShell’s own way to kill a process (although they prefer to use the word “Stop” rather than killing!). Stop-Process is a cmdlet that performs similar to the TASKKILL command but gives you a slightly different set of options.
The Syntax for the Stop-Process command is as follows:
- Stop-Process [-id] <Int32[]> [-passThru] [-Force] [WhatIf][-confirm] [<CommonParameters>]
- Stop-Process -name string[] [-passThru] [-Force] [-WhatIf] [-confirm] [<CommonParameters>]
- Stop-Process -inputObject Process[] [-passThru] [-WhatIf] [-Force] [-confirm] [<CommonParameters>]
TASKKILL vs Stop-Process?
PowerShell’s Stop-Process, on the other hand, helps you create an autonomous task with scripting powers. For example, the “-passthru” parameter allows you to return objects from commands, which you can later use for scripting. The Stop-Process also includes two risk mitigation parameters (-WhatIf) and (-Confirm) to avoid the Stop-Process from making dramatic changes to the system.
A Stop-Process Example?
We’ll kill the notepad process forcefully, first by defining its PID, and then by its process name.
Step 1. Find it
Use the “Tasklist” command to see all your processes (or create a filter).
Step 2. Stop the process with its PID:
Optional: You can also stop the process using its name. For example:
- Stop-process -name notepad -Force
- Stop-Process -name CMD -Force
- Stop-process -name putty -Force
- Stop-Process -Name Chrome -Force
To return a process object to the PowerShell console, use the PassThru command. This command is useful for keeping track of process objects. For example, if you want to monitor the newly opened Notepad process.
The below screenshot starts the process with a -PassThru parameter and later stops the same process with a -PassThru. You can store the returned process object into a variable and use it to monitor metrics like CPU or PM, etc.
Conclusion
Buggy or problematic services and applications can be overwhelming. They are time-consuming to manage and can make entire servers slow— or even crash them.
Instead of having to every time restart the entire server, you can kill the specific service that is giving you headaches, and get the server back to normal.
In this tutorial, we went through two ways to kill a process using PowerShell. The TASKKILL is simple and easy to use. You can connect to remote servers and kill processes using its PID or name. The PowerShell’s Stop-Process cmdlet can do the same but goes beyond by allowing you to automate tasks. With Stop-Process, you can create auto-restart scripts that monitor and kill risky processes.
PowerShell Kill Process Command FAQs
Can I use TASKKILL to terminate multiple processes at once?
How can I find the process ID of a process?
You can use the Task Manager to find the process ID of a process. In Task Manager, select the “View” menu, then select “Select Columns,” and then check the “PID (Process Identifier)” check box. Or you can use the command line tool tasklist in the command prompt, which will list the running process with their pid.
What happens when I use the TASKKILL command?
When you use the TASKKILL command, the specified process is terminated and all resources associated with it are freed up.
Can I use TASKKILL to terminate a process that is not responding?
Yes, you can use the /F option with the TASKKILL command to force the termination of a process that is not responding.
I think the cmdlet aspect is fine. But for processes, I’m struggling. In the below example if I take away the -wait from running notepad, it returns a process ID (which i could then kill) but of course the job finishes instantly.
If I put the -wait in, when it times out it returns nothing because the start-process command hasn’t finished. So I can’t kill the launched process (notepad.exe).
Any pointers would be helpful – thanks.
$timeoutSeconds = 5
function Start-My-Process {
param($ScriptBlock, $TimeoutSeconds)
$job = Start-Job -ScriptBlock $ScriptBlock
if($job | Wait-Job -Timeout $TimeoutSeconds){
$returnCode = $job | Receive-Job
} else {
write-host "timed out"
#cannot get process ID to kill?
#& taskkill /PID $returnCode /f /t
}
$job |Remove-Job -Force
}
$code = {
#start-sleep -Seconds 3
return (start-process notepad.exe -passthru -wait).Id
}
Start-My-Process $code $timeoutSeconds
asked Dec 20, 2023 at 15:00
1 gold badge14 silver badges32 bronze badges
- You don’t need to kill any console-application child process launched by direct invocation from your job explicitly – it is automatically killed as part of (forcefully) removing the enclosing job with
Remove-Job -Force
.
cmd /c Notepad
Otherwise, use
Start-Process
with-PassThru
to launch the application and output the resulting process-information object from the job, where the caller can retrieve it viaReceive-Job
, and obtain the PID (process ID) from its.Id
property.
You can also use this technique if you want to asynchronously launch console applications from your job (at the expense of being able to directly capture their output).
Finally, note that UWP applications (typically obtained from Microsoft Store) are often launched in a manner where the original process delegates to a different, non-child process and then exits right away, which makes them impossible to track based on the original process’ ID.
answered Dec 20, 2023 at 17:28
67 gold badges673 silver badges862 bronze badges