Stop-Process

Stop one or more running processes. (Kill)

Syntax
      Stop-Process -name string[] [-passThru] [-Force]
            [-confirm] [-whatIf] [CommonParameters]
    
      Stop-Process [-id] Int32[] [-passThru] [-Force]
            [-confirm] [-whatIf] [CommonParameters]
    
      Stop-Process -inputObject Process[] [-passThru] [-Force]
            [-confirm] [-whatIf] [CommonParameters]
Key
   -Name 
       Process name(s)
       Separate multiple process names with commas or use wildcard characters. 

   -id Int32
       Process ID(s) (PID). Use commas to separate multiple PIDs.
       To find the PID of a process, type "get-process".
   
   -inputObject 
       Accept a process object as input to Stop-Process.  
       A variable, command or expression that returns the process object(s)

   -PassThru 
       Pass the object created by Stop-Process along the pipeline. 

   -Force
       Stop the specified processes without prompting for confirmation.
       By default, Stop-Process prompts for confirmation before stopping
       any process that is not owned by the current user.

       To find the owner of a process, use Get-WmiMethod to get
       a Win32_Process object that represents the process, and then
       use the GetOwner method of the object.

   -WhatIf
       Describe what would happen if you executed the command without
       actually executing the command.

   -Confirm
       Prompt for confirmation before executing the command.

Standard Aliases for Stop-Process: kill, spps

Stop-Process works only on processes running on the local computer.

On Vista and later versions of Windows, to stop a process that is not owned by the current user, you must start PowerShell with "Run as administrator".

Graceful termination

In many cases you can ask the process to exit normally before killing it, example via StackOverflow [x]
Some applications (Skype, MS Teams) will stubbornly remain open until Stop-Process -Force is used.

# get Firefox process
$firefox = Get-Process firefox -ErrorAction SilentlyContinue
if ($firefox) {
  # try gracefully first
  $firefox.CloseMainWindow()

  # kill after five seconds
  Sleep 5
  if (!$firefox.HasExited) {
    $firefox | Stop-Process -Force
  }
}
Remove-Variable firefox

Examples

Stop all instances of the Notepad process:

PS C:\> stop-process -name notepad

Stop process ID# 6464 and prompt before stopping the process (this will display the process name first):

PS C:\> stop-process -id 6464 -confirm -passthru

Stop the PowerShell process itself ($PID) this will immediately exit PowerShell:

PS C:\> stop-process -id $PID

Display processes that were running on the computer, but are now stopped:

PS C:\> get-process | where-object {$_.HasExited}

"Whom the gods love dies young" ~ Menander 300 BC

Related PowerShell Cmdlets

Invoke-Command - Run commands on local and remote computers.
Start-Process - Start one or more processes, optionally as a specific user.
Get-Process - Get a list of processes on a machine.
Start-Process - Start one or more processes.
--% - Stop parsing input.
Equivalent bash command: kill - Stop a process from running.


 
Copyright © 1999-2024 SS64.com
Some rights reserved