Set-Service

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

语法

Name (默认值)

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

InputObject

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 参数使用值 Running 来启动服务。 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[]

默认值:Local computer
支持通配符:False
不显示:False
别名:cn

参数集

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

-Confirm

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

参数属性

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

参数集

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

-Description

指定服务的新说明。

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

参数属性

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

参数集

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

-DisplayName

指定服务的新显示名称。

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:DN

参数集

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

-InputObject

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

参数属性

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

参数集

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

-Name

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

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:服务名称, SN

参数集

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

-PassThru

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

参数属性

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

参数集

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

-StartupType

指定服务的启动模式。

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

  • 自动。 系统启动时启动。
  • 已禁用。 无法启动服务。
  • 手动。 仅在用户或程序启动时才启动。

参数属性

类型:ServiceStartMode
默认值:None
接受的值:Automatic, Boot, Disabled, Manual, System
支持通配符:False
不显示:False
别名:StartMode, SM, ST

参数集

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

-Status

说明服务的状态。

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

  • 暂停。 暂停服务。
  • Running。 启动服务。
  • Stopped。 停止服务。

参数属性

类型:String
默认值:None
接受的值:Paused, Running, Stopped
支持通配符:False
不显示:False

参数集

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

-WhatIf

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

参数属性

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

参数集

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

CommonParameters

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

输入

System.ServiceProcess.ServiceController, System.String

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

输出

ServiceController

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

备注

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

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

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