Home » PowerShell Tips » Stop-Process – Kill Proces using PowerShell

Stop-Process – Kill Proces using PowerShell

Stop-Process PowerShell cmdlet stop process if running. You can kill the process if running using process name, process Id (PID), or process object to Stop-Process.

More often, we have an application that stops responding, and even after waiting for a time, it still displays a program not responding message.

There are ways to kill the process if running using task manager to select the process and kill it.

Another option is to use PowerShell Stop-Process cmdlet to kill a process by PID, or Process name or kill the process if running.

In this article, I will explain how to stop the process if running in PowerShell using process name, process id on the local system.

Stop-Process PowerShell cmdlet

Syntax

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

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

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

Parameters:

Id – Process Id (PID), use commas to separate multiple Id

Name – Process Name, use commas to separate multiple process names or use wildcard characters to get process name

InputObject – process object returned by variable or command.

PassThru – Pass the object created by Stop-Process over the pipeline to another command

Force – forcefully stop the process without prompt confirmation

Confirm – Prompt for confirmation to execute the command.

Note: PowerShell Stop-Process cmdlet only stops processes running on the local computer.

Let’s understand Stop-Process in PowerShell with examples.

Kill Process if running by name using PowerShell

You can kill all instances of the process if running by process name on the local computer using the Stop-Process command as below

Stop-Process -Name "notepad"

Above command, use Name parameter to specify the process name as notepad.

Stop-Process kills all the instances of a notepad application.

Each instance of the notepad application has its own PID. It will stop all instances of a notepad application as it has the same name.

Cool Tip: How to use base64 encode the file in PowerShell!

Stop Process if running by PID using PowerShell

You can kill the process by process id using Stop-Process as given below

Stop-Process -Id 38592 -Confirm -PassThru

In the above PowerShell command, Stop-Process uses PID to kill the process specified by process PID.

Confirm parameter prompt for confirmation to perform the action or not. If you select, Yes or Yes to All, it will stop a process if running.

Stop running process
Stop running process

On Confirmation, it will terminate the process and print output as below

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                       
-------  ------    -----      -----     ------     --  -- -----------                                                       
    247      15     3360      15164       0.27  38592   1 notepad  

Stop Process using Process Object

If you want to stop process using a process object returned by command or stored in a variable, use the InputObject parameter to pass an object to the Stop-Process cmdlet.

Use Get-Process cmdlet gets a process and store process object in a variable, run below command

# Get Process and store object in variable
$ProcessObject = Get-Process -Name notepad

# Pass object to Stop-Process using InputObject parameter
Stop-Process -InputObject $ProcessObject

# Check if process stopped or not
Get-Process | Where-Object {$_.HasExited}

In the above PowerShell command, the Stop-Process PowerShell cmdlet kill process and next command check if the killed process still exists or not.

Stop Process Not Owned by the Current User

If you want to stop process not owned by the current user, you will need to open PowerShell with the Run as administrator option.

Using the Force the parameter will terminate the process.

If you try to kill process not owned by the current user without an administrator option will display a message as

Stop-Process : Cannot stop process 'notepad(1353)' because of the following error: Access is denied
At line:1 char:3

Use the following script to stop process not owned by the current user

Get-Process -Name notepad | Stop-Process -Force

The first command gets the process notepad using the Get-Process cmdlet and passes it to the second command.

The second command uses the Stop-Process cmdlet to kill the process object forcefully using the Force parameter.

Note: Open PowerShell run as administrator to stop process. Only administrator group members can stop process not owned by the current user.

Conclusion

I hope the above article helps you to use the Stop-Process PowerShell cmdlet to kill process specified by name or process id (PID) or kill process not owned by the current user.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment