Get-Service

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

语法

Default (默认值)

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

DisplayName

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

InputObject

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-Service 参数不使用 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。 属性 参数显示服务的名称、依赖服务的名称和显示每个服务的依赖服务数的计算属性。

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

此示例显示,按服务 状态 属性的值按升序对服务进行排序时,停止的服务会显示在运行服务之前。 发生这种情况是因为 状态 的值是一个枚举,其中 Stopped 具有 1的值,Running 具有 4的值。 有关详细信息,请参阅 ServiceControllerStatus

若要首先列出正在运行的服务,请使用 cmdlet 的 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

示例 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 在两台远程计算机和本地计算机(Get-Service Winrm)上运行 localhost 命令。

该命令在远程计算机上运行,结果将返回到本地计算机。 管道运算符(|)将结果发送到 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-Service

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False
别名:Cn

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-DependentServices

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

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:DS

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-DisplayName

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

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False

参数集

DisplayName
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Exclude

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

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Include

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

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-InputObject

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

参数属性

类型:

ServiceController[]

默认值:None
支持通配符:False
不显示:False

参数集

InputObject
Position:Named
必需:False
来自管道的值:True
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Name

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

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False
别名:服务名称

参数集

Default
Position:0
必需:False
来自管道的值:True
来自管道的值(按属性名称):True
来自剩余参数的值:False

-RequiredServices

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

参数属性

类型:SwitchParameter
默认值:False
支持通配符:True
不显示:False
别名:SDO, ServicesDependedOn

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters

输入

ServiceController

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

String

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

输出

ServiceController

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

备注

Windows PowerShell 为 Get-Service提供以下别名:

  • gsv

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

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

注释

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

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

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