How to Kill a Process by Name in PowerShell

To kill a process by name in PowerShell, run Stop-Process -Name followed by the process name, without .exe. It stops every running process with that name:

Stop-Process -Name notepad

For the tests in this guide, I started a few hidden ping.exe processes, so nothing real was harmed. Here the command stops two of them:

"Running before: $(@(Get-Process -Name PING).Count)"

Stop-Process -Name PING
Wait-Process -Name PING -ErrorAction SilentlyContinue

"Running after:  $(@(Get-Process -Name PING -ErrorAction SilentlyContinue).Count)"

Output:

Running before: 2
Running after:  0

I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. Only the last trick, killing a whole process tree with one method call, is PowerShell 7 only.

Find the right process name

The name Stop-Process expects is the ProcessName from Get-Process. It usually matches the .exe file without the extension, but not always what Task Manager shows. Check first:

Get-Process | Sort-Object -Property CPU -Descending | Select-Object -First 10 -Property Name, Id, CPU

Names aren’t case-sensitive, so ping and PING work the same. Wildcards like chrome* work too, but preview them with -WhatIf first, as shown below.

How do I kill a process only if it’s running?

If no process has that name, Stop-Process throws an error:

try {
    Stop-Process -Name PING -ErrorAction Stop
}
catch {
    $_.Exception.Message
}

Output:

Cannot find a process with the name "PING". Verify the process name and call the cmdlet again.

In a script, check with Get-Process first and stop only what it finds. I wrapped that in a small function and called it twice:

function Stop-IfRunning {
    param([string]$Name)

    $processes = Get-Process -Name $Name -ErrorAction SilentlyContinue
    if ($processes) {
        $processes | Stop-Process
        "Stopped $(@($processes).Count) $Name process(es)."
    }
    else {
        "$Name isn't running. Nothing to do."
    }
}

Stop-IfRunning -Name PING
Stop-IfRunning -Name PING

Output:

Stopped 2 PING process(es).
PING isn't running. Nothing to do.
PowerShell kill a process only if it is running with Get-Process and Stop-Process
Stopping a process only when it’s running (PowerShell 7)

The first call stopped both processes. The second found nothing and said so, without an error. To check without stopping anything, see how to check if a process is running.

Preview what will be stopped with -WhatIf

-WhatIf lists each process Stop-Process would kill, with its ID, and stops nothing. Use it before any wildcard or bulk command:

Stop-Process -Name PING -WhatIf

Output (your process IDs will differ):

What if: Performing the operation "Stop-Process" on target "PING (37992)".
What if: Performing the operation "Stop-Process" on target "PING (57296)".
PowerShell Stop-Process by name with -WhatIf in Windows PowerShell 5.1
Previewing Stop-Process in Windows PowerShell 5.1

Does -Force kill a process harder?

No. This is a common mix-up. Stop-Process always ends the process immediately, the same way End task does in Task Manager, and any unsaved work in it is lost.

-Force only skips the confirmation prompt that appears when you stop a process owned by another user. To stop those processes at all, you also need to run PowerShell as administrator.

Be careful with system processes. Microsoft warns that stopping a process can stop services that depend on it, and in the worst case, Windows itself.

Kill a process by ID

When several processes share a name and you only want one of them, use its process ID. Here I start a process, keep its object, and stop exactly that one:

$ping = Start-Process ping.exe -ArgumentList '-n', '120', '127.0.0.1' -WindowStyle Hidden -PassThru

Stop-Process -Id $ping.Id
$ping.WaitForExit()

"Exited: $($ping.HasExited), exit code: $($ping.ExitCode)"

Output:

Exited: True, exit code: -1

Stop-Process doesn’t wait for the process to exit, so call WaitForExit() or Wait-Process before you rely on it being gone. An exit code of -1 means the process was killed rather than closing normally.

For more ways to start processes and capture their IDs, see PowerShell Start-Process.

Kill processes that have been running too long

Because Get-Process returns objects, you can filter on any property before stopping. This stops only the process that started more than 3 seconds ago:

$cutoff = (Get-Date).AddSeconds(-3)

$stuck = Get-Process -Name PING | Where-Object { $_.StartTime -and $_.StartTime -lt $cutoff }
$stuck | Stop-Process
Wait-Process -InputObject $stuck

"Stopped $(@($stuck).Count), still running: $(@(Get-Process -Name PING).Count)"

Output:

Stopped 1, still running: 1

In real use, you’d use minutes or hours, like (Get-Date).AddHours(-2), to clean up stuck jobs.

Keep the $_.StartTime -and check. StartTime is empty for processes you can’t read, and PowerShell treats an empty value as older than any date. When I tested the filter without it, the Windows System process matched.

Stop a process and its child processes

Stop-Process stops only the process you name. Anything it started keeps running. Here, killing a cmd.exe left its ping behind, while taskkill /T stopped both:

$parent = Start-Process cmd.exe -ArgumentList '/c', 'ping -n 120 127.0.0.1 > nul' -WindowStyle Hidden -PassThru
Start-Sleep -Seconds 1
Stop-Process -Id $parent.Id
Start-Sleep -Milliseconds 500
"After Stop-Process: $(@(Get-Process -Name PING -ErrorAction SilentlyContinue).Count) PING still running"

Get-Process -Name PING | Stop-Process

$parent = Start-Process cmd.exe -ArgumentList '/c', 'ping -n 120 127.0.0.1 > nul' -WindowStyle Hidden -PassThru
Start-Sleep -Seconds 1
taskkill /PID $parent.Id /T /F | Out-Null
Start-Sleep -Milliseconds 500
"After taskkill /T:  $(@(Get-Process -Name PING -ErrorAction SilentlyContinue).Count) PING still running"

Output:

After Stop-Process: 1 PING still running
After taskkill /T:  0 PING still running
PowerShell Stop-Process leaves child processes running while taskkill /T stops the process tree
Stop-Process vs taskkill /T (PowerShell 7)

/T means the whole tree, and /F forces it, which matches what Stop-Process does. Both work in PowerShell 7 and 5.1.

PowerShell 7: Kill($true)

In PowerShell 7, the process object can stop its whole tree in one call:

$parent = Start-Process cmd.exe -ArgumentList '/c', 'ping -n 120 127.0.0.1 > nul' -WindowStyle Hidden -PassThru
Start-Sleep -Seconds 1

$parent.Kill($true)
Start-Sleep -Milliseconds 500
"PING still running: $(@(Get-Process -Name PING -ErrorAction SilentlyContinue).Count)"

Output in PowerShell 7:

PING still running: 0

Windows PowerShell 5.1 runs on the older .NET Framework, which doesn’t have that option:

$parent = Start-Process cmd.exe -ArgumentList '/c', 'ping -n 120 127.0.0.1 > nul' -WindowStyle Hidden -PassThru
Start-Sleep -Seconds 1
try {
    $parent.Kill($true)
}
catch {
    $_.Exception.Message
}
taskkill /PID $parent.Id /T /F | Out-Null

Output in Windows PowerShell 5.1:

Cannot find an overload for "Kill" and the argument count: "1".

For scripts that must run in both versions, stick with taskkill /T /F. The Stop-Process documentation covers the remaining parameters.

Frequently Asked Questions

How do I kill a process by name in PowerShell?

Run Stop-Process -Name processname, without .exe. It stops every process with that name. kill is a built-in alias on Windows.

How do I stop a process only if it’s running?

Get it first with Get-Process -Name name -ErrorAction SilentlyContinue, then pipe the result to Stop-Process only if something was found.

What does -Force do in Stop-Process?

It skips the confirmation prompt for processes owned by another user. It doesn’t make the kill stronger, because Stop-Process always ends the process immediately.

Does Stop-Process kill child processes?

No. Use taskkill /PID id /T /F to stop the whole tree. In PowerShell 7, $process.Kill($true) does the same.

Why do I get “Access is denied” when stopping a process?

The process belongs to another user or to Windows. Run PowerShell as administrator, and double-check the process before stopping it.

For more process management, read these next:

Leave a Comment