I like to think that the person who came up with the kill command named it after having a process freeze on them. In a fit of rage, they started hurling threats at the process and mumbling under their breath, "I'm gonna kill you…"
At least, that's how I like to imagine it went down. The actual sequence of events was probably much less dramatic. Regardless of how it came to be named, it's what it does that counts.
Here, we’ll introduce you to the PowerShell equivalent to taskkill and provide you with examples of how you can put it into action.
The kill command in Windows operating systems is taskkill, which sounds equally as aggressive. The taskkill command is used to terminate processes, which is especially useful when a program decides to stop responding to you. If you've ever tried to close an application only to have it freeze on you, you know how instantly aggravating it can be. Closing should be the most simple task an application can perform, yet it's surprising how often applications will just decide to hang on to your desktop for dear life.
I'm sure there are a plethora of reasons why this can occur, but I'm not interested in reasons. I'm interested in results. Keep in mind that you may need to add the force (/F) to kill a process that isn't responding. Using the force switch is similar to launching Task Manager and ending the task from there.
Of course, there are several other reasons why you would want to use the taskkill command, especially if you are deploying updates. In fact, if you look at several of the pre-built packages in PDQ Deploy's package library, they contain a command step that uses the taskkill utility to end an application's process if it's currently running before the update will proceed.
Here's an example of the FIleZilla package from the package library.
The taskkill command is pretty straightforward to run. As always, if you want to find out more about a command, add the /? switch after the command, which will return the help documents about the command. The most common use is to run the command followed by either the process name, also known as image name (/IM), or the process ID (/PID).
Another useful command to use when using taskkill is the tasklist command. If you don't know the name or the process ID of the task you want to terminate, tasklist is what you need. Tasklist will return a list of all the processes currently running, listing both the process name and process ID.
Of course, we're not here to discuss old command prompt utilities, so let's find out what the PowerShell equivalent command of taskkill is.
Okay, I'll be the first to admit it; the name is a little lackluster. Especially when compared to TASKKILL!!!!!! Stop-Process just doesn't carry the same hostile undertones Thankfully, Microsoft at least gave us kill as an alias, so we've got that going for us. Regardless, let's see if it still packs the same task-killing punch.
You'll notice that we have some familiar switches available with the Stop-Process command, such as -ID and -Name. We also have the -Force switch for those times we need to strong-arm a process into submission. Let's see what we can do with a few examples.
This is the most basic example of this command, but there is something interesting to note. Because we are using the -Name parameter, this command will close all instances of notepad.exe that are running. You can have several instances of notepad running simultaneously, and each of those instances will have a unique process ID.
If you use the -ID parameter instead of -Name, you can specify which instance you want to terminate. Also, note my use of the Get-Process command instead of tasklist in the image above. These commands serve similar functions, and it might be a command we cover more in-depth in the future.
In this example, we've got multiple processes we want to stop. First, we've taken the process names and assigned them to the variable $List. Next, we use a ForEach loop to iterate through each $Item in the $List variable. We've also gone hardcore and used the kill alias instead of Stop-Process, because why not? Lastly, we've used the -Force parameter just in case any of these processes try to go rogue and not close like they're supposed to.
In this example, we'll check to see if a process is running before terminating it. First, we'll assign the process name to a variable $app so we can check for other processes easily in the future. 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/else statement to stop the process if it is running, and if it's not running, return the output $app "not running."
Stopping processes is one of those things we do dozens of times a day, and we probably don't even realize it. I can't tell you how many times I close Chrome or a PowerShell window just to open it back up again two minutes later. It works flawlessly 99% of the time. But, the one percent when it doesn't can sure be frustrating. Well, at least now you have one more way to handle those stubborn processes.
Are you a fan of PowerShell? So are we (just in case you couldn't tell). Consider subscribing to our blog to make sure you always get notified when we post new articles covering PowerShell, security vulnerabilities, product guides, and more.
Born in the '80s and raised by his NES, Brock quickly fell in love with everything tech. With over 15 years of IT experience, Brock now enjoys the life of luxury as a renowned tech blogger and receiver of many Dundie Awards. In his free time, Brock enjoys adventuring with his wife, kids, and dogs, while dreaming of retirement.