Stop Process - PowerShell


Overview

The Stop-Process command forcibly halts running processes in the local or remote machines. It is useful for managing unruly processes that may be consuming excessive resources or causing system instability.

Syntax

Stop-Process [-ProcessName] <String> [-Id] <Int32> [-ComputerName] <String> [-PassThru] [-Force] [-WhatIf] [-Confirm] [-ThrottleLimit] <Int32>

Options/Flags

  • -ProcessName: The name of the process to stop.
  • -Id: The process ID of the process to stop.
  • -ComputerName: The name of the remote computer where the process is running.
  • -PassThru: Returns an object representing the stopped process.
  • -Force: Terminates the process without waiting for it to close.
  • -WhatIf: Simulates the command and displays what would happen if it were executed.
  • -Confirm: Prompts for confirmation before stopping the process.
  • -ThrottleLimit: Limits the number of processes that can be stopped simultaneously.

Examples

Simple Example

Stop-Process -ProcessName "notepad.exe"

Complex Example

$remoteProcesses = Get-Process -ComputerName "RemoteServer"
ForEach ($process in $remoteProcesses) {
  Stop-Process -ProcessName $process.Name -ComputerName $process.ComputerName -Force
}

Common Issues

  • Access denied: You may encounter this error if the command is executed without proper permissions. Ensure you have administrative privileges or sufficient permissions to terminate the process.
  • Process not found: If the specified process name or ID does not exist, the command will fail with this error. Verify the process details before attempting to stop it.

Integration

Stop-Process can be used in conjunction with other commands to automate process management tasks. For example:

Get-Process | Where-Object {$_.CPU -gt 80} | Stop-Process