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>]
说明
此 cmdlet 仅在 Windows 平台上可用。
Get-Service
cmdlet 获取表示计算机上的服务的对象,包括正在运行和停止的服务。 默认情况下,在没有参数的情况下运行 Get-Service
时,将返回所有本地计算机的服务。
可以通过指定服务名称或服务的显示名称来指示此 cmdlet 仅获取特定服务,也可以通过管道将服务对象传递给此 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 仅选择 状态 属性等于 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。
属性 参数显示服务的名称、依赖服务的名称和显示每个服务的依赖服务数的计算属性。
示例 7:按属性值对服务进行排序
此示例显示,按服务 状态 属性的值按升序对服务进行排序时,停止的服务会显示在运行服务之前。 发生这种情况是因为 状态 的值是一个枚举,其中 Stopped
具有 1
的值,Running
具有 4
的值。 有关详细信息,请参阅 ServiceControllerStatus。
若要首先列出正在运行的服务,请使用 Sort-Object
参数。
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。