取得本機或遠端電腦上的服務。

語法

Get-Service [[-Name] <string[]>] [-ComputerName <string[]>] [-DependentServices] [-Exclude <string[]>] [-Include <string[]>] [-RequiredServices] [<CommonParameters>]

Get-Service -DisplayName <string[]> [-ComputerName <string[]>] [-DependentServices] [-Exclude <string[]>] [-Include <string[]>] [-RequiredServices] [<CommonParameters>]

Get-Service [-InputObject <ServiceController[]>] [-ComputerName <string[]>] [-DependentServices] [-Exclude <string[]>] [-Include <string[]>] [-RequiredServices] [<CommonParameters>]

描述

Get-Service Cmdlet 會取得代表本機或遠端電腦上之服務的物件,其中包括正在執行與已經停止的服務。

您可以指定服務名稱或服務的顯示名稱,藉此要求 Get-Service 只取得特定服務,或者可以透過管線將服務物件傳遞給 Get-Service。

參數

-ComputerName <string[]>

取得於指定電腦上執行的服務。預設為本機電腦。

請輸入遠端電腦的 NetBIOS 名稱、IP 位址或完整網域名稱。若要指定本機電腦,請輸入電腦名稱、句點 (.) 或 "localhost"。

此參數並不依存於 Windows PowerShell 遠端。即使沒有將電腦設定成執行遠端命令,也可以使用 Get-Service 的 ComputerName 參數。

必要?

false

位置?

named

預設值

Localhost

接受管線輸入?

true (ByPropertyName)

接受萬用字元?

false

-DependentServices

只取得依存於指定之服務的服務。

根據預設,Get-Service 會取得所有服務。

必要?

false

位置?

named

預設值

False

接受管線輸入?

false

接受萬用字元?

false

-DisplayName <string[]>

指定要擷取之服務的顯示名稱。允許使用萬用字元。根據預設,Get-Service 會取得電腦上的所有服務。

必要?

true

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Exclude <string[]>

忽略指定的服務。此參數的值用於限定 Name 參數。輸入名稱元素或模式,例如 "s*"。允許使用萬用字元。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Include <string[]>

只擷取指定的服務。此參數的值用於限定 Name 參數。輸入名稱元素或模式,例如 "s*"。允許使用萬用字元。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-InputObject <ServiceController[]>

指定表示要擷取之服務的 ServiceController 物件。請輸入包含物件的變數,或輸入可取得物件的命令或運算式。您也可以透過管線將服務物件傳遞給 Get-Service。

必要?

false

位置?

named

預設值

接受管線輸入?

true (ByValue)

接受萬用字元?

false

-Name <string[]>

指定要擷取之服務的服務名稱。允許使用萬用字元。根據預設,Get-Service 會取得電腦上的所有服務。

必要?

false

位置?

1

預設值

接受管線輸入?

true (ByValue, ByPropertyName)

接受萬用字元?

true

-RequiredServices

只取得這個服務所需的服務。

這個參數會取得此服務的 ServicesDependedOn 屬性值。根據預設,Get-Service 會取得所有服務。

必要?

false

位置?

named

預設值

False

接受管線輸入?

false

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

System.ServiceProcess.ServiceController、System.String

您可以透過管線將服務物件或服務名稱傳遞給 Get-Service。

輸出

System.ServiceProcess.ServiceController

Get-Service 會傳回代表電腦上之服務的物件。

附註

您也可以利用內建的別名 "gsv" 來參照 Get-Service。如需詳細資訊,請參閱 about_Aliases。

Get-Service 只會在目前使用者具有查看服務權限時才會顯示這些服務。如果 Get-Service 沒有顯示服務,表示您可能沒有檢視這些服務的權限。

若要找出系統上各個服務的服務名稱和顯示名稱,請輸入 "get-service"。此服務名稱顯示於 Name 欄,顯示名稱則顯示於 DisplayName 欄。

根據狀態值以遞增順序排序時,"Stopped" 服務會出現在 "Running" 服務之前。服務的 Status 屬性為一個列舉值,其中狀態名稱以整數值表示。因此,排序是根據整數值,而不是名稱。"Running" 排列在 "Stopped" 之前是因為 "Stopped" 的值為 "1",而 "Running" 的值為 "4"。

範例 1

C:\PS>get-service

描述
-----------
這個命令會擷取系統上的所有服務。它的作用就像輸入 "get-service *" 一樣。預設顯示會列出每個服務的狀態、服務名稱和顯示名稱。






範例 2

C:\PS>get-service wmi*

描述
-----------
這個命令會擷取服務名稱以 "WMI" 開頭的服務 (WMI 是 Windows Management Instrumentation 的縮寫)。






範例 3

C:\PS>get-service -displayname *network*

描述
-----------
這個命令顯示在顯示名稱中包含 "network" 一字的服務
。搜尋顯示名稱會尋找網路相關的服務 (即使該服務名稱不包含 "Net"),例如,它會找出表示 Network Provisioning Service 的 xmlprov。






範例 4

C:\PS>get-service -name win* -exclude winrm

描述
-----------
這些命令只會取得服務名稱以 "win" 開頭的服務,但是 WinRM 服務除外。






範例 5

C:\PS>get-service | where-object {$_.Status -eq "Running"}

描述
-----------
這個命令只會顯示目前正在執行的服務。該命令會使用 Get-Service Cmdlet 取得電腦上的所有服務。管線運算子 (|) 會將結果傳遞給 Where-Object Cmdlet,此 Cmdlet 只會選取 Status 屬性等於 "Running" 的服務。

Status 只是其中一種服務物件屬性。若要查看所有屬性,請輸入 "get-service | get-member"。






範例 6

C:\PS>get-service -computername Server02

描述
-----------
這個命令會取得 Server02 遠端電腦上的服務。

因為 Get-Service 的 ComputerName 參數不使用 Windows PowerShell 遠端,所以即使電腦沒有設定為使用 Windows PowerShell 中的遠端時,您仍然可以使用這個參數。






範例 7

C:\PS>get-service | where-object {$_.DependentServices} | format-list -property Name, DependentServices, @{Label="NoOfDependentS
ervices"; Expression={$_.dependentservices.count}} 

Name                  : AudioEndpointBuilder
DependentServices     : {AudioSrv}
NoOfDependentServices : 1

Name                  : Dhcp
DependentServices     : {WinHttpAutoProxySvc}
NoOfDependentServices : 1
...

描述
-----------
這些命令會列出電腦上具有相依服務的服務。

第一個命令使用 Get-Service Cmdlet 取得電腦上的服務。管線運算子 (|) 會將服務傳遞給 Where-Object Cmdlet,該 Cmdlet 會選取屬性不為 Null 的服務。

另一個管線運算子會將結果傳送到 Format-List Cmdlet。命令使用它的 Property 參數顯示服務名稱、相依服務的名稱以及顯示每個服務所擁有之相依服務數量的計算屬性。






範例 8

C:\PS>C:\PS> get-service s* | sort-object status 

Status   Name               DisplayName
------   ----               -----------
Stopped  stisvc             Windows Image Acquisition (WIA)
Stopped  SwPrv              MS Software Shadow Copy Provider
Stopped  SysmonLog          Performance Logs and Alerts
Running  Spooler            Print Spooler
Running  srservice          System Restore Service
Running  SSDPSRV            SSDP Discovery Service
Running  ShellHWDetection   Shell Hardware Detection
Running  Schedule           Task Scheduler
Running  SCardSvr           Smart Card
Running  SamSs              Security Accounts Manager
Running  SharedAccess       Windows Firewall/Internet Connectio...
Running  SENS               System Event Notification
Running  seclogon           Secondary Logon

C:\PS> get-service s* | sort-object status -descending

Status   Name               DisplayName
------   ----               -----------
Running  ShellHWDetection   Shell Hardware Detection
Running  SharedAccess       Windows Firewall/Internet Connectio...
Running  Spooler            Print Spooler
Running  SSDPSRV            SSDP Discovery Service
Running  srservice          System Restore Service
Running  SCardSvr           Smart Card
Running  SamSs              Security Accounts Manager
Running  Schedule           Task Scheduler
Running  SENS               System Event Notification
Running  seclogon           Secondary Logon
Stopped  SysmonLog          Performance Logs and Alerts
Stopped  SwPrv              MS Software Shadow Copy Provider
Stopped  stisvc             Windows Image Acquisition (WIA)

描述
-----------
這個命令顯示在您根據服務 Status 屬性值以遞增順序排序服務時,已停止的服務會出現在執行中服務之前。這種情況發生的原因是 Status 的值屬於列舉型別,其中 "Stopped" 的值為 "1",而 "Running" 的值為 "4"。

若要列出執行中的服務,請使用 Sort-Object Cmdlet 的 Descending 參數。






範例 9

C:\PS>get-service -name winrm -computername localhost, Server01, Server02  | format-table -property MachineName, Status, Name, DisplayName -auto

MachineName    Status  Name  DisplayName
------------   ------  ----  -----------
localhost      Running WinRM Windows Remote Management (WS-Management)
Server01       Running WinRM Windows Remote Management (WS-Management)
Server02       Running WinRM Windows Remote Management (WS-Management)

描述
-----------
這個命令使用 Get-Service Cmdlet 在兩部遠端電腦與本機電腦 ("localhost") 上執行 "Get-Service Winrm" 命令。

Get-Service 命令於遠端電腦上執行,並且結果會傳回到本機電腦。管線運算子 (|) 將結果傳送到 Format-Table Cmdlet,該 Cmdlet 會以表格方式呈現服務的資料。Format-Table 命令使用 Property 參數,指定顯示於表格中的屬性,包括 MachineName 屬性。






範例 10

C:\PS>get-service winrm -requiredServices

描述
-----------
這個命令會取得 WinRM 服務所需的服務。

此命令會傳回服務的 ServicesDependedOn 屬性值。






範例 11

C:\PS>"winrm" | get-service

描述
-----------
這個命令會取得本機電腦上的 WinRM 服務。這個範例示範您可以透過管線將服務名稱字串 (以引號括住) 傳遞給 Get-Service。






請參閱




目錄