Share via


使用 Process Cmdlet 管理進程

此範例僅適用於 Windows PowerShell 5.1。

您可以使用 PowerShell 中的 Process Cmdlet 來管理 PowerShell 中的本機和遠端進程。

取得進程

若要取得在本機電腦上執行的進程,請執行 Get-Process 不含參數的 。

您可以藉由指定其行程名稱或進程識別碼來取得特定進程。 下列命令會取得閑置進程:

Get-Process -id 0
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
      0       0        0         16     0               0 Idle

雖然 Cmdlet 在某些情況下不會傳回任何數據,但是當您依 其 ProcessId 指定進程時,如果找不到相符專案, Get-Process 就會產生錯誤,因為通常的意圖是擷取已知的執行中進程。 如果沒有具有該識別碼的進程,可能是識別碼不正確或感興趣的程式已經結束:

Get-Process -Id 99
Get-Process : No process with process ID 99 was found.
At line:1 char:12
+ Get-Process  <<<< -Id 99

您可以使用 Cmdlet 的 Get-Process Name 參數,根據行程名稱指定進程子集。 Name 參數可以在逗號分隔清單中採用多個名稱,而且它支援使用通配符,因此您可以輸入名稱模式。

例如,下列命令會取得名稱開頭為 「ex」 的進程。

Get-Process -Name ex*
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    234       7     5572      12484   134     2.98   1684 EXCEL
    555      15    34500      12384   134   105.25    728 explorer

因為 .NET System.Diagnostics.Process 類別是 PowerShell 程式的基礎,所以它會遵循 System.Diagnostics.Process 所使用的一些慣例。 其中一個慣例是可執行檔案的行程名稱永遠不會在可執行檔案名稱結尾包含 .exe

Get-Process 也接受 Name 參數的多個值。

Get-Process -Name exp*,power*
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    540      15    35172      48148   141    88.44    408 explorer
    605       9    30668      29800   155     7.11   3052 powershell

您可以使用的 Get-Process ComputerName 參數來取得遠端電腦上的進程。 例如,下列命令會取得本機計算機上的PowerShell進程(以 “localhost” 表示)和兩部遠端電腦上。

Get-Process -Name PowerShell -ComputerName localhost, Server01, Server02
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    258       8    29772      38636   130            3700 powershell
    398      24    75988      76800   572            5816 powershell
    605       9    30668      29800   155     7.11   3052 powershell

計算機名稱在此顯示中並不明顯,但會儲存在傳回之進程物件的 Get-Process MachineName屬性中。 下列命令會 Format-Table 使用 Cmdlet 來顯示進程對象的進程 標識碼ProcessNameMachineName (ComputerName ) 屬性。

Get-Process -Name PowerShell -ComputerName localhost, Server01, Server01 |
    Format-Table -Property ID, ProcessName, MachineName
  Id ProcessName MachineName
  -- ----------- -----------
3700 powershell  Server01
3052 powershell  Server02
5816 powershell  localhost

這個更複雜的命令會將 MachineName 屬性新增至標準 Get-Process 顯示器。

Get-Process powershell -ComputerName localhost, Server01, Server02 |
    Format-Table -Property Handles,
        @{Label="NPM(K)";Expression={[int]($_.NPM/1024)}},
        @{Label="PM(K)";Expression={[int]($_.PM/1024)}},
        @{Label="WS(K)";Expression={[int]($_.WS/1024)}},
        @{Label="VM(M)";Expression={[int]($_.VM/1MB)}},
        @{Label="CPU(s)";Expression={if ($_.CPU -ne $()){$_.CPU.ToString("N")}}},
        Id, ProcessName, MachineName -auto
Handles  NPM(K)  PM(K) WS(K) VM(M) CPU(s)  Id ProcessName  MachineName
-------  ------  ----- ----- ----- ------  -- -----------  -----------
    258       8  29772 38636   130         3700 powershell Server01
    398      24  75988 76800   572         5816 powershell localhost
    605       9  30668 29800   155 7.11    3052 powershell Server02

停止進程

PowerShell 可讓您彈性列出進程,但停止進程呢?

Cmdlet Stop-Process 會採用 [名稱 ] 或 [標識符 ] 來指定您想要停止的進程。 您停止進程的能力取決於您的許可權。 某些進程無法停止。 例如,如果您嘗試停止閑置的進程,就會收到錯誤:

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 參數強制提示。 如果您在指定行程名稱時使用通配符,這個參數特別有用,因為您可能會不小心比對一些您不想停止的進程:

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 物件具有 在不再回應時為 true 的 Responding 屬性,因此您可以使用下列命令停止所有非回應的應用程式:

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

在其他情況下,您可以使用相同的方法。 例如,假設當用戶啟動另一個應用程式時,次要通知區域應用程式會自動執行。 您可能會發現,這在終端機服務會話中無法正常運作,但仍想要將它保留在實體計算機控制臺上執行的會話中。 線上到實體電腦桌面的會話一律有 0 的會話識別碼,因此您可以使用 和 SessionId,停止其他工作階段Where-Object中進程的所有實例:

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

Cmdlet Stop-Process 沒有 ComputerName 參數。 因此,若要在遠端電腦上執行停止進程命令,您必須使用 Invoke-Command Cmdlet。 例如,若要停止 Server01 遠端電腦上的 PowerShell 進程,請輸入:

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

停止所有其他 PowerShell 工作階段

有時候,能夠停止目前會話以外的所有執行中的PowerShell會話可能很實用。 如果工作階段使用太多資源或無法存取(它可能從遠端或在另一個桌面工作階段中執行),您可能無法直接停止它。 不過,如果您嘗試停止所有執行中的會話,則目前的會話可能會改為終止。

每個 PowerShell 會話都有環境變數 PID,其中包含 Windows PowerShell 程式的標識碼 。 您可以針對每個會話的標識符檢查$PID,並只終止具有不同標識符的 Windows PowerShell 會話。下列管線命令會執行這項作業,並傳回已終止會話的清單(因為使用 PassThru 參數):

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

啟動、偵錯和等候進程

PowerShell 也隨附 Cmdlet 來啟動(或重新啟動)、偵錯進程,並等候進程在執行命令之前完成。 如需這些 Cmdlet 的相關信息,請參閱每個 Cmdlet 的 Cmdlet 說明主題。

另請參閱