Skip to main content
PDQ.com mobilePDQ.com desktop
Support
Try Now

What Is The PowerShell Equivalent Of Taskkill

Brock BinghamBrock Bingham
·

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.

Table Of Contents

The Taskkill Command

The kill command in Windows operating systems is taskkill, which is equally as aggressive sounding.  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).

taskkill /IM notepad.exe

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.

The PowerShell Equivalent Of Taskkill Is Stop-Process

NAME
    Stop-Process

SYNTAX
    Stop-Process [-Id] <int[]> [-PassThru] [-Force] [-WhatIf] [-Confirm]  [<CommonParameters>]

    Stop-Process -Name <string[]> [-PassThru] [-Force] [-WhatIf] [-Confirm]  [<CommonParameters>]

    Stop-Process [-InputObject] <Process[]> [-PassThru] [-Force] [-WhatIf] [-Confirm]  [<CommonParameters>]


ALIASES
    spps
    kill


REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help Stop-Process -Online" or
           go to https://go.microsoft.com/fwlink/?LinkID=113412.

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 overtones with it.  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.

Stop-Process Example 1

Stop-Process -Name 'notepad'

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.

Stop-Process Example 2

$List = 'notepad', 'win32calc', 'chrome'

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.

Stop-Process Example 3

$app = 'Chrome'

$process = Get-Process $app -ErrorAction SilentlyContinue
if ($process) {
  Stop-Process $process -Force
  Write-Output "$app has been stopped."}
  else {
  Write-Output "$app is not running."}

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."

Wrapping Up

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.  Ninety-nine percent of the time, it works flawlessly.  But that one percent when it doesn't can sure be frustrating.  It always seems to happen at the worst times as well, like when you're trying to get out the door to beat the rush-hour traffic.  Well, at least now you have one more way to handle those pesky 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.

Ready to put the PowerShell Scanner through its paces?

Take our 14-day Free Trial. Feel the power of the PowerShell scanner.
Start a Trial

Don't miss the next post!

SeriousSAM - Vulnerability Exposes Registry Data Files To Low Privileged Users

Microsoft continues to keep us on our toes. Their latest vulnerability is granting standard users access to sensitive Registry database files. Time for a workaround.

PDQ.com
© 2021 PDQ.com Corporation
Products
  • PDQ Deploy ®
  • PDQ Inventory ®
  • Enterprise SL
  • Pricing
  • Downloads
  • Licensing
  • Buy