如何使用PowerShell强制终止进程?

时间:2017-04-07 20:55:52

标签: windows powershell process

我有一个我想杀的过程:

$ ps Sourcetree

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
   1311     116   136720     137804      57.45   1044   1 SourceTree

将其发送至Stop-Process, aka kill

$ ps Sourcetree | kill

运行没有错误。但这个过程并未被杀死:

$ ps Sourcetree

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
   1311     116   136720     137804      57.45   1044   1 SourceTree

如何使用PowerShell强制终止进程?

3 个答案:

答案 0 :(得分:1)

取自help Stop-Process -online

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

-Force

Stops 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 the Get-WmiObject cmdlet to
get a Win32_Process object that represents the process, and then
use the GetOwner method of the object.
Type: SwitchParameter
Parameter Sets: (All)
Aliases: 

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

Bonus nachos,Get-Command -ParameterName *force将显示包含强制参数的所有Cmdlet。

答案 1 :(得分:0)

为了更好地了解cmdlet以及它们的功能,您始终可以Get-Help cmdlet查看它支持的内容。

Get-Help Stop-Process

或者只是将cmdlet输入搜索引擎,这很可能会引导您访问官方帮助文章。

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/stop-process

答案 2 :(得分:0)

如果你这样做?

(get-process -Name SourceTree).Kill()