Windows PowerShell 可以灵活地列出进程,但停止进程呢?
Stop-Process cmdlet 将采用 Name 或 ID 来指定想要停止的进程。 能否停止进程取决于你的权限。 某些进程无法停止。 例如,如果你尝试停止空闲进程,则会出现错误:

PS> Stop-Process -Name Idle
Stop-Process : Process 'Idle (0)' cannot be stopped due to the following error:
 Access is denied
At line:1 char:13
+ Stop-Process  <<<< -Name Idle

也可以通过使用 Confirm 参数进行强制提示。 如果你在指定进程名称时使用通配符,此参数会特别有用,因为可能会意外地匹配到你不想要停止的一些进程:

PS> Stop-Process -Name t*,e* -Confirm
Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "explorer (408)".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):n
Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "taskmgr (4072)".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):n

可以通过使用某些对象筛选 cmdlet 来实现复杂的过程操作。 因为 Process 对象具有 Responding 属性,当程序不再响应时该属性为 true,你可以使用以下命令停止所有无响应的应用程序:

Get-Process | Where-Object -FilterScript {$_.Responding -eq $false} | Stop-Process

在其他情况下,可以使用相同的方法。 例如,假设用户启动另一个应用程序时,辅助通知区域的应用程序将自动运行。 你可能会发现在“终端服务”会话中它无法正常工作,但你仍想要将其保留在物理计算机控制台上运行的会话中。 连接到物理计算机桌面的会话始终具有一个为 0 的会话 ID,这样你可以通过使用 Where-Object 和进程 SessionId 停止在其他会话中的所有进程的实例:

Get-Process -Name BadApp | Where-Object -FilterScript {$_.SessionId -neq 0} | Stop-Process

Stop-Process cmdlet 不具有 ComputerName 参数。 因此,若要在远程计算机上运行停止进程,需要使用 Invoke-Command cmdlet。 例如,若要停止 Server01 远程计算器上的 PowerShell 进程,请键入:

Invoke-Command -ComputerName Server01 {Stop-Process Powershell}

停止所有其他 Windows PowerShell 会话

这可能对停止当前会话之外的所有正在运行的 Windows PowerShell 会话偶尔有用。 如果会话正在使用过多资源或无法访问(它可能正在远程运行或在另一个桌面会话中运行),可能不能直接将其停止。 如果你尝试停止所有正在运行的会话,但是,这可能会终止当前会话。
每个 Windows PowerShell 会话都具有一个包含 Windows PowerShell 进程的 ID 的环境变量 PID。 你可以对比每个会话的 ID 检查 $PID,并仅终止具有不同 ID 的 Windows PowerShell 会话。下面的管道命令执行此操作并返回终止的会话的列表(因为使用了 PassThru 参数):

PS> Get-Process -Name powershell | Where-Object -FilterScript {$_.Id -ne $PID} | Stop-Process -PassThru

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    334       9    23348      29136   143     1.03    388 powershell
    304       9    23152      29040   143     1.03    632 powershell
    302       9    20916      26804   143     1.03   1116 powershell
    335       9    25656      31412   143     1.09   3452 powershell
    303       9    23156      29044   143     1.05   3608 powershell
    287       9    21044      26928   143     1.02   3672 powershell