Get-Service
取得電腦上的服務。
語法
Get-Service
[[-Name] <String[]>]
[-DependentServices]
[-RequiredServices]
[-Include <String[]>]
[-Exclude <String[]>]
[<CommonParameters>]
Get-Service
[-DependentServices]
[-RequiredServices]
-DisplayName <String[]>
[-Include <String[]>]
[-Exclude <String[]>]
[<CommonParameters>]
Get-Service
[-DependentServices]
[-RequiredServices]
[-Include <String[]>]
[-Exclude <String[]>]
[-InputObject <ServiceController[]>]
[<CommonParameters>]
Description
此 Cmdlet 只能在 Windows 平臺上使用。
Get-Service
Cmdlet 會取得物件,這些物件代表計算機上的服務,包括執行和停止的服務。 根據預設,在沒有參數的情況下執行 Get-Service
時,會傳回所有本機計算機的服務。
您可以指定服務名稱或顯示名稱,或者將服務物件透過管線傳送給此 cmdlet,以取得特定的服務。
範例
範例 1:取得計算機上的所有服務
這個範例會取得計算機上的所有服務。 其行為就像您輸入 Get-Service *
一樣。 默認顯示會顯示每個服務的狀態、服務名稱和顯示名稱。
Get-Service
範例 2:取得以搜尋字串開頭的服務
此範例會擷取服務名稱開頭為 WMI
(Windows Management Instrumentation) 的服務。
Get-Service "wmi*"
範例 3:顯示包含搜尋字串的服務
本範例會以包含 network
一字的顯示名稱來顯示服務。 即使服務名稱不包含 Net
,例如 xmlprov、網路布建服務,搜尋顯示名稱仍會尋找網路相關服務。
Get-Service -DisplayName "*network*"
範例 4:取得以搜尋字串和排除項目開頭的服務
此範例只會取得服務名稱開頭為 win
的服務,但 WinRM 服務除外。
Get-Service -Name "win*" -Exclude "WinRM"
範例 5:顯示目前使用中的服務
此範例只會顯示狀態為 Running
的服務。
Get-Service | Where-Object {$_.Status -eq "Running"}
Get-Service
取得計算機上的所有服務,並將對象傳送至管線。
Where-Object
Cmdlet 只會選取 Status 屬性等於 Running
的服務。
Status 只是服務物件的一個屬性。 若要檢視所有屬性,請輸入 Get-Service | Get-Member
。
範例 6:列出計算機上具有相依服務的服務
此範例會取得具有相依服務的服務。
Get-Service | Where-Object {$_.DependentServices} |
Format-List -Property Name, DependentServices, @{
Label="NoOfDependentServices"
Expression={$_.DependentServices.Count}
}
Name : AudioEndpointBuilder
DependentServices : {AudioSrv}
NoOfDependentServices : 1
Name : Dhcp
DependentServices : {WinHttpAutoProxySvc}
NoOfDependentServices : 1
...
Get-Service
Cmdlet 會取得計算機上的所有服務,並將對象傳送至管線。
Where-Object
cmdlet 會選取 DependentServices 屬性不是 null 的服務。
結果會向下傳送至 Format-List
Cmdlet。
Property 參數會顯示服務的名稱、相依服務的名稱,以及顯示每個服務的相依服務數目的導出屬性。
範例 7:依屬性值排序服務
此範例顯示,當您依其 Status 屬性的值,依遞增順序排序服務時,停止的服務會出現在執行服務之前。 這是因為 Status 的值是列舉,其中 Stopped
值為 1
,而 Running
的 值為 4
。 如需詳細資訊,請參閱 ServiceControllerStatus。
若要先列出執行中的服務,請使用 Sort-Object
Cmdlet 的 Descending 參數。
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
範例 8:取得服務的依賴服務
此範例會取得 WinRM 服務所需的服務。 返回服務的 ServicesDependedOn 屬性值。
Get-Service "WinRM" -RequiredServices
範例 9:透過管線運算元取得服務
此範例會取得本機電腦上的 WinRM 服務。 服務名稱字串會被以引號括住,並沿著管線傳送至 Get-Service
。
"WinRM" | Get-Service
參數
-DependentServices
表示此 Cmdlet 只會取得相依於指定服務的服務。
類型: | SwitchParameter |
別名: | DS |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-DisplayName
指定要擷取之服務的顯示名稱,做為字串陣列。 允許通配符。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | False |
接受萬用字元: | True |
-Exclude
指定一個或多個服務的字串陣列,以排除此 Cmdlet 作業中的服務。
此參數的值用來說明 Name 參數。 輸入名稱元素或模式,例如 s*
。 允許通配符。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
-Include
指定一個或多個服務,以字串陣列的形式由此 Cmdlet 包含在操作中。 此參數的值會限定 Name 參數。 輸入名稱元素或模式,例如 s*
。 允許通配符。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
-InputObject
指定要擷取之服務的 ServiceController 物件。 輸入包含 物件的變數,或輸入取得物件的命令或表達式。 您可以使用管道將服務物件傳送至此 Cmdlet。
類型: | ServiceController[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | False |
-Name
指定要擷取的服務服務名稱。 允許通配符。
類型: | String[] |
別名: | ServiceName |
Position: | 0 |
預設值: | None |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | True |
-RequiredServices
表示此 Cmdlet 只會取得此服務所需的服務。 此參數會取得服務的 ServicesDependedOn 屬性值。
類型: | SwitchParameter |
別名: | SDO, ServicesDependedOn |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
輸入
您可以透過管線將服務物件傳送至此 Cmdlet。
您可以使用管線將服務名稱傳送至此 cmdlet。
輸出
此 Cmdlet 會傳回代表電腦上服務的物件。
備註
PowerShell 包含下列 Get-Service
別名:
- 窗戶:
gsv
此 Cmdlet 僅適用於 Windows 平臺。
從 PowerShell 6.0 開始,下列屬性會新增至 ServiceController 物件:UserName、Description、DelayedAutoStart、BinaryPathName和 StartupType 。
此 Cmdlet 只有在目前用戶有權查看服務時,才能顯示服務。 如果此 Cmdlet 未顯示服務,您可能沒有看到這些服務的許可權。
若要尋找系統上每個服務的服務名稱和顯示名稱,請輸入 Get-Service
。 服務名稱會出現在 [Name] 資料行中,而顯示名稱會出現在 DisplayName 數據行中。
附註
一般而言,Get-Service
會傳回服務的相關信息,而不是驅動程式。 不過,如果您指定驅動程式的名稱,Get-Service
會傳回驅動程式的相關信息。
- 列舉不包含設備驅動器服務
- 指定通配符時,Cmdlet 只會傳回 Windows 服務
- 如果您指定與裝置服務名稱完全相符的 Name 或 DisplayName,則會傳回裝置實例
當您依狀態值以遞增順序排序時,Stopped
服務會出現在 Running
服務之前。 服務的 Status 屬性是列舉值,其中狀態的名稱代表整數值。 排序是以整數值為基礎,而不是名稱。
Running
會出現在 Stopped
之前,因為 Stopped
具有 1
的值,而 Running
的 值為 4
。 如需詳細資訊,請參閱 ServiceControllerStatus。