Get-Service

获取本地或远程计算机上的服务。

语法

Get-Service
   [[-Name] <String[]>]
   [-ComputerName <String[]>]
   [-DependentServices]
   [-RequiredServices]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [<CommonParameters>]
Get-Service
   [-ComputerName <String[]>]
   [-DependentServices]
   [-RequiredServices]
   -DisplayName <String[]>
   [-Include <String[]>]
   [-Exclude <String[]>]
   [<CommonParameters>]
Get-Service
   [-ComputerName <String[]>]
   [-DependentServices]
   [-RequiredServices]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-InputObject <ServiceController[]>]
   [<CommonParameters>]

说明

Get-Service cmdlet 获取表示本地计算机或远程计算机上的服务的对象,包括正在运行和停止的服务。

可以通过指定服务名称或服务的显示名称来指示此 cmdlet 仅获取特定服务,也可以通过管道将服务对象传递给此 cmdlet。

示例

示例 1:获取计算机上的所有服务

Get-Service

此命令获取计算机上的所有服务。 它的行为就像你键入 Get-Service *一样。 默认显示显示每个服务的状态、服务名称和显示名称。

示例 2:获取以搜索字符串开头的服务

Get-Service "wmi*"

此命令检索服务名称以 WMI 开头(Windows Management Instrumentation 的首字母缩略词)。

示例 3:显示包含搜索字符串的服务

Get-Service -Displayname "*network*"

此命令显示包含单词网络的显示名称的服务。 搜索显示名称会查找与网络相关的服务,即使服务名称不包含“Net”,例如 xmlprov、网络预配服务。

示例 4:获取以搜索字符串和排除项开头的服务

Get-Service -Name "win*" -Exclude "WinRM"

这些命令仅获取服务名称以 win 开头的服务,但 WinRM 服务除外。

示例 5:显示当前处于活动状态的服务

Get-Service | Where-Object {$_.Status -eq "Running"}

此命令仅显示当前处于活动状态的服务。 它使用 Get-Service cmdlet 来获取计算机上的所有服务。 管道运算符 (|) 将结果传递给 Where-Object cmdlet,该 cmdlet 仅选择状态属性等于“正在运行”的服务。

Status 只是服务对象的一个属性。 若要查看所有属性,请键入 Get-Service | Get-Member

示例 6:获取远程计算机上的服务

Get-Service -ComputerName "Server02"

此命令获取 Server02 远程计算机上的服务。

由于 Get-ServiceComputerName 参数不使用 Windows PowerShell 远程处理,因此即使计算机未在 Windows PowerShell 中配置远程处理,也可以使用此参数。

示例 7:列出具有依赖服务的本地计算机上的服务

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,该 cmdlet 选择其 DependentServices 属性不为 null 的服务。

另一个管道运算符将结果发送到 Format-List cmdlet。 该命令使用其 属性 参数来显示服务的名称、依赖服务的名称,以及显示每个服务具有的依赖服务数的计算属性。

示例 8:按属性值对服务进行排序

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

此命令显示,按服务 状态 属性的值按升序对服务进行排序时,停止的服务会显示在运行服务之前。 发生这种情况的原因是状态值是一个枚举,其中“已停止”的值为 1,而“正在运行”的值为 4。

若要首先列出正在运行的服务,请使用 Sort-Object cmdlet 的降序 参数。

示例 9:获取多台计算机上的服务

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 命令。

该命令在远程计算机上运行,结果将返回到本地计算机。 管道运算符 (|) 将结果发送到 Format-Table cmdlet,该 cmdlet 将服务格式化为表。 Format-Table 命令使用 Property 参数指定表中显示的属性,包括 MachineName 属性。

示例 10:获取服务的依赖服务

Get-Service "WinRM" -RequiredServices

此命令获取 WinRM 服务所需的服务。

该命令返回服务的 ServicesDependedOn 属性的值。

示例 11:通过管道操作员获取服务

"WinRM" | Get-Service

此命令获取本地计算机上的 WinRM 服务。 此示例演示如何通过管道将服务名称字符串(用引号括起来)Get-Service

参数

-ComputerName

获取在指定计算机上运行的服务。 默认值为本地计算机。

键入远程计算机的 NetBIOS 名称、IP 地址或完全限定的域名(FQDN)。 若要指定本地计算机,请键入计算机名称、点(.)或 localhost。

此参数不依赖于 Windows PowerShell 远程处理。 即使计算机未配置为运行远程命令,也可以使用 Get-ServiceComputerName 参数。

类型:String[]
别名:Cn
Position:Named
默认值:None
必需:False
接受管道输入:True
接受通配符:False

-DependentServices

指示此 cmdlet 仅获取依赖于指定服务的服务。

默认情况下,此 cmdlet 获取所有服务。

类型:SwitchParameter
别名:DS
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-DisplayName

指定要检索的服务的显示名称,作为字符串数组。 允许通配符。 默认情况下,此 cmdlet 将获取计算机上的所有服务。

类型: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

指定要检索的服务的服务名称。 允许通配符。 默认情况下,此 cmdlet 获取计算机上的所有服务。

类型:String[]
别名:ServiceName
Position:0
默认值:None
必需:False
接受管道输入:True
接受通配符:True

-RequiredServices

指示此 cmdlet 仅获取此服务所需的服务。

此参数获取服务的 ServicesDependedOn 属性的值。 默认情况下,此 cmdlet 获取所有服务。

类型:SwitchParameter
别名:SDO, ServicesDependedOn
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:True

输入

System.ServiceProcess.ServiceController, System.String

可以通过管道将服务对象或服务名称传递给此 cmdlet。

输出

ServiceController

此 cmdlet 返回表示计算机上的服务的对象。

备注

还可以通过其内置别名“gsv”来引用 Get-Service。 有关详细信息,请参阅about_Aliases。

仅当当前用户有权查看服务时,此 cmdlet 才能显示服务。 如果此 cmdlet 不显示服务,则可能无权查看它们。

若要查找系统上每个服务的服务名称和显示名称,请键入 Get-Service。 服务名称显示在“名称”列中,显示名称显示在 DisplayName 列中。

按状态值按升序排序时,“已停止”服务会显示在“正在运行”服务之前。 服务的 Status 属性是一个枚举值,其中状态的名称表示整数值。 排序基于整数值,而不是名称。 “正在运行”显示在“已停止”之前,因为“Stopped”的值为“1”,而“正在运行”的值为“4”。