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 获取表示本地计算机或远程计算机上的服务(包括正在运行和已停止的服务)的对象。 默认情况下,在没有参数的情况下运行 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 仅选择 Status 属性等于 Running 的服务。

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

结果沿着管道发送到 Format-List cmdlet。 Property 参数显示服务的名称、依赖服务的名称以及显示每个服务的依赖服务数的计算属性。

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

此示例显示按服务的 Status 属性值以升序对服务进行排序时,已停止的服务将出现在正在运行的服务之前。 发生这种情况是因为 Status 的值是一个枚举,其中 Stopped 的值为 1Running 的值为 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

示例 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:获取服务的依赖服务

此示例获取 WinRM 服务所需的服务。 返回服务的 ServicesDependedOn 属性的值。

Get-Service "WinRM" -RequiredServices

示例 11:通过管道运算符获取服务

此示例获取本地计算机上的 WinRM 服务。 服务名称字符串(括在引号中)沿着管道发送到 Get-Service

"WinRM" | Get-Service

参数

-ComputerName

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

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

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

Type:String[]
Aliases:Cn
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-DependentServices

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

Type:SwitchParameter
Aliases:DS
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-DisplayName

以字符串数组的形式指定要检索的服务的显示名称。 允许使用通配符。

Type:String[]
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:True

-Exclude

以字符串数组的形式指定此 cmdlet 从操作中排除的一个或多个服务。 此参数值使 Name 参数有效。 输入名称元素或模式,例如 s*。 允许使用通配符。

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-Include

以字符串数组的形式指定此 cmdlet 包含在操作中的一个或多个服务。 此参数值使 Name 参数有效。 输入名称元素或模式,例如 s*。 允许使用通配符。

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-InputObject

指定表示要检索的服务的 ServiceController 对象。 输入一个包含对象的变量,或键入可获取对象的命令或表达式。 可以通过管道将服务对象传递给此 cmdlet。

Type:ServiceController[]
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Name

指定要检索的服务的名称。 允许使用通配符。

Type:String[]
Aliases:ServiceName
Position:0
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:True

-RequiredServices

指示此 cmdlet 仅获取此服务所需的服务。 此参数获取服务的 ServicesDependedOn 属性值。

Type:SwitchParameter
Aliases:SDO, ServicesDependedOn
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:True

输入

ServiceController

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

String

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

输出

ServiceController

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

备注

Windows PowerShell 包含以下 Get-Service 别名:

  • gsv

此 cmdlet 只有在当前用户有权查看服务的情况下才显示服务。 如果此 cmdlet 不显示服务,则你可能无权查看它们。

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

注意

通常, Get-Service 返回有关服务的信息,而不是驱动程序。 但是,如果指定驱动程序的名称, Get-Service 则返回有关驱动程序的信息。

  • 枚举不包括设备驱动程序服务
  • 指定 wild卡 时,cmdlet 仅返回 Windows 服务
  • 如果指定与设备服务名称完全匹配的名称或 DisplayName,则返回设备实例

按状态值按升序排序时,Stopped 服务显示在 Running 服务之前。 服务的 Status 属性是一个枚举值,其中状态的名称表示整数值。 该排序是基于整数值进行的,而非名称。 Running 显示在 Stopped 之前,因为 Stopped 的值为 1Running 的值为 4。 有关详细信息,请参阅 ServiceControllerStatus