The Stop-Process PowerShell cmdlet stops the process if running. You can kill the process if running by using the process name, process ID (PID), or process object with the Stop-Process cmdlet in PowerShell.
There are ways to kill the process if running using:
- task manager to select the process and kill it.
- the 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 the process name, and process ID on the local system.
PowerShell Stop-Process 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>]
Note: The PowerShell Stop-Process cmdlet only stops processes running on the local computer.
Let’s understand Stop-Process in PowerShell with examples.
How to Kill Process if Running by Process Name Using PowerShell
To kill the process if running by process name, use the Stop-Process
cmdlet with the Name
parameter. The Name parameter specifies the process name running on the local system.
Stop-Process -Name "notepad"
In the above PowerShell script, the Stop-Process
command uses the Name parameter to specify the process name as “notepad“.
The Stop-Process
cmdlet in PowerShell 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!
How to Stop Process if Running by PID Using PowerShell
To stop the process if running by PID (process id), use the Stop-Process
command with a Id
parameter. The Id
parameter is used to specify the process ID.
Stop-Process -Id 38592 -Confirm -PassThru
In the above PowerShell command, the Stop-Process
uses Id parameter to kill the process specified by process PID.
The Confirm
parameter prompts for confirmation to perform the action or not. If you select, Yes or Yes to All, it will stop a process if running.
On Confirmation, it will terminate the process and print the output below.
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
247 15 3360 15164 0.27 38592 1 notepad
How to Kill Process If Exists Using PowerShell
To kill the process if exists using PowerShell, follow the below steps:
- Open the PowerShell terminal.
- Get the list of running processes using the Get-Process command and store them in the
$processes
variable. - Check if the process that you want to kill is running by using the
Where-Object
cmdlet to filter the list of running processes and store the result in the$process
variable. - If the process is running, kill it using the
Stop-Process
cmdlet.
The following PowerShell script checks kill the process if exists.
# Get Process and store object in variable $processes = Get-Process # Check if the process that you want to kill is exists and running $process = $processes | Where-Object {$_.Name -eq "notepad"} # If the process is exists and running, kill it if($process){ Stop-Process -Name "notepad" }
How to Stop a Process Not Owned by the Current User Using PowerShell
To stop a process that is not owned by the current user, you will need to open PowerShell with the Run as administrator option.
Using the Force parameter with the Stop-Proess to terminate the process.
Use the following script to stop a process that is not owned by the current user.
Get-Process -Name notepad | Stop-Process -Force
The Get-Process
command gets the process notepad and passes it to the Stop-Process
command.
The Stop-Process
cmdlet kills the process object forcefully using the Force
parameter.
Note: Open the PowerShell terminal with administrator privileges to stop the process. Only administrator group members can stop processes not owned by the current user.
If you try to kill a 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
Cool Tip: How to get-process user name in PowerShell!
PowerShell Stop-Process Parameters
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.
Conclusion
I hope the above article helps you to use the Stop-Process PowerShell cmdlet to kill a process specified by name or process ID (PID) or kill a 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.