Set-Service

启动、停止和挂起服务,并更改其属性。

语法

Set-Service
   [-Name] <String>
   [-ComputerName <String[]>]
   [-DisplayName <String>]
   [-Description <String>]
   [-StartupType <ServiceStartMode>]
   [-Status <String>]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
Set-Service
   [-ComputerName <String[]>]
   [-DisplayName <String>]
   [-Description <String>]
   [-StartupType <ServiceStartMode>]
   [-Status <String>]
   [-InputObject <ServiceController>]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

说明

Set-Service cmdlet 更改服务的属性,例如 状态DescriptionDisplayNameStartupTypeSet-Service 可以启动、停止、暂停或暂停服务。 若要标识服务,请输入其服务名称或提交服务对象。 或者,将服务名称或服务对象向下发送到管道 Set-Service

示例

示例 1:更改显示名称

在此示例中,服务的显示名称已更改。 若要查看原始显示名称,请使用 Get-Service

Set-Service -Name LanmanWorkstation -DisplayName "LanMan Workstation"

Set-Service 使用 Name 参数来指定服务的名称,LanmanWorkstationDisplayName 参数指定新的显示名称,LanMan Workstation

示例 2:更改服务的启动类型

此示例演示如何更改服务的启动类型。

Set-Service -Name BITS -StartupType Automatic
Get-Service BITS | Select-Object -Property Name, StartType, Status

Name  StartType   Status
----  ---------   ------
BITS  Automatic  Running

Set-Service 使用 Name 参数指定服务的名称,BITSStartupType 参数将服务设置为 自动

Get-Service 使用 Name 参数指定 BITS 服务,并将对象发送到管道。 Select-Object 使用 属性 参数显示 BITS 服务的状态。

示例 3:更改服务的说明

此示例更改 BITS 服务的说明并显示结果。

使用 Get-CimInstance cmdlet,因为它返回 Win32_Service 对象,该对象包含服务的 Description

Get-CimInstance Win32_Service -Filter 'Name = "BITS"'  | Format-List  Name, Description

Name        : BITS
Description : Transfers files in the background using idle network bandwidth. If the service is
              disabled, then any applications that depend on BITS, such as Windows Update or MSN
              Explorer, will be unable to automatically download programs and other information.

Set-Service -Name BITS -Description "Transfers files in the background using idle network bandwidth."
Get-CimInstance Win32_Service -Filter 'Name = "BITS"' | Format-List  Name, Description

Name        : BITS
Description : Transfers files in the background using idle network bandwidth.

Get-CimInstance 将对象向下发送到管道 Format-List,并显示服务的名称和说明。 出于比较目的,命令在更新说明之前和之后运行。

Set-Service 使用 Name 参数来指定 BITS 服务。 Description 参数指定服务说明的更新文本。

示例 4:启动服务

在此示例中,将启动服务。

Set-Service -Name WinRM -Status Running -PassThru

Status   Name               DisplayName
------   ----               -----------
Running  WinRM              Windows Remote Management (WS-Manag...

Set-Service 使用 Name 参数来指定服务,WinRMStatus 参数使用 运行 的值来启动服务。 PassThru 参数输出显示结果的 ServiceController 对象。

示例 5:暂停服务

此示例使用管道暂停到服务。

Get-Service -Name Schedule | Set-Service -Status Paused

Get-Service 使用 Name 参数来指定 计划 服务,并将对象发送到管道。 Set-Service 使用 Status 参数将服务设置为 暂停

示例 6:停止服务

此示例使用变量来停止服务。

$S = Get-Service -Name Schedule
Set-Service -InputObject $S -Status Stopped

Get-Service 使用 Name 参数来指定服务,Schedule。 对象存储在变量中,$SSet-Service 使用 InputObject 参数并指定存储 $S的对象。 Status 参数将服务设置为 已停止

参数

-ComputerName

指定一台或多台计算机。 对于远程计算机,请键入 NetBIOS 名称、IP 地址或完全限定的域名。 如果未指定 ComputerName 参数,则命令在本地计算机上运行。

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

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

-Confirm

在运行 Set-Service之前,提示你进行确认。

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

-Description

指定服务的新说明。

服务说明显示在 计算机管理、服务中。 Description 不是 Get-ServiceServiceController 对象的属性。 若要查看服务说明,请使用返回表示服务的 Win32_Service 对象的 Get-CimInstance

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

-DisplayName

指定服务的新显示名称。

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

-InputObject

指定一个 ServiceController 对象,该对象表示要更改的服务。 输入包含对象的变量,或键入获取对象的命令或表达式,例如 Get-Service 命令。 可以使用管道将服务对象发送到 Set-Service

类型:ServiceController
Position:Named
默认值:None
必需:False
接受管道输入:True
接受通配符:False

-Name

指定要更改的服务的服务名称。 不允许使用通配符。 可以使用管道将服务名称发送到 Set-Service

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

-PassThru

返回一个 ServiceController 对象,该对象表示已更改的服务。 默认情况下,Set-Service 不会生成任何输出。

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

-StartupType

指定服务的启动模式。

此参数的可接受值如下所示:

  • 自动。 系统启动时启动。
  • 禁用。 无法启动服务。
  • 手动。 仅在用户或程序启动时才启动。
类型:ServiceStartMode
别名:StartMode, SM, ST
接受的值:Automatic, Boot, Disabled, Manual, System
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-Status

指定服务的状态。

此参数的可接受值如下所示:

  • 暂停。 暂停服务。
  • 运行。 启动服务。
  • 已停止。 停止服务。
类型:String
接受的值:Paused, Running, Stopped
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-WhatIf

显示 Set-Service 运行时会发生什么情况。 该 cmdlet 未运行。

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

输入

System.ServiceProcess.ServiceController, System.String

可以使用管道将服务对象或包含服务名称的字符串发送到 Set-Service

输出

ServiceController

默认情况下,Set-Service 不返回任何对象。 使用 PassThru 参数输出 ServiceController 对象。

备注

Set-Service 需要提升的权限。 使用 以管理员身份运行 选项。

Set-Service 只能在当前用户有权管理服务时控制服务。 如果命令无法正常工作,则可能没有所需的权限。

若要查找服务的服务名称或显示名称,请使用 Get-Service。 服务名称位于 名称 列中,显示名称位于 DisplayName 列中。