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

语法

Set-Service [-Name] <string> [-Description <string>] [-DisplayName <string>] [-PassThru] [-StartupType {<Automatic> | <Manual> | <Disabled>}] [-Status <string>] [-ComputerName <string[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

Set-Service [-Description <string>] [-DisplayName <string>] [-InputObject <ServiceController>] [-PassThru] [-StartupType {<Automatic> | <Manual> | <Disabled>}] [-Status <string>] [-ComputerName <string[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

说明

Set-Service cmdlet 可更改本地或远程服务的属性,其中包括状态、说明、显示名称和启动模式。可以使用此 cmdlet 启动、停止或挂起(暂停)服务。若要标识服务,请输入其服务名称或提交服务对象,或者通过管道将服务名称或服务对象传递给 Set-Service。

参数

-ComputerName <string[]>

指定一台或多台计算机。默认值为本地计算机。

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

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

是否为必需?

false

位置?

named

默认值

local computer

是否接受管道输入?

true (ByPropertyName)

是否接受通配符?

false

-Description <string>

指定服务的新说明。

服务说明出现在计算机管理的“服务”中。Description 不是 Get-Service 所获取的 ServiceController 对象的属性。要查看服务说明,请使用 Get-WmiObject 获取表示服务的 Win32_Service 对象。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-DisplayName <string>

指定服务的新显示名称。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-InputObject <ServiceController>

指定 ServiceController 对象来表示要更改的服务。请输入包含该对象的变量,或者键入获取该对象的命令或表达式(如 Get-Service 命令)。也可以将服务对象通过管道传递给 Set-Service。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

true (ByValue)

是否接受通配符?

false

-Name <string>

指定要更改的服务的服务名称。不允许使用通配符。还可以通过管道将服务名称传递给 Set-Service。

是否为必需?

true

位置?

1

默认值

是否接受管道输入?

true (ByValue, ByPropertyName)

是否接受通配符?

false

-PassThru

返回表示已更改的服务的对象。默认情况下,此 cmdlet 将不产生任何输出。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-StartupType <ServiceStartMode>

更改服务的启动模式。StartupType 的有效值包括:

-- Automatic:在系统启动时启动。

-- Manual:仅在由用户或程序启动时才启动。

-- Disabled:无法启动。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-Status <string>

启动、停止或挂起(暂停)服务。有效值包括:

-- Running:启动服务。

-- Stopped:停止服务。

-- Paused:挂起服务。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-Confirm

在执行命令之前提示您进行确认。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-WhatIf

描述如果执行该命令会发生什么情况(无需实际执行该命令)。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

<CommonParameters>

此 cmdlet 支持通用参数:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。有关详细信息,请参阅 about_Commonparameters.

输入和输出

输入类型是指可通过管道传递给 cmdlet 的对象的类型。返回类型是指 Cmdlet 所返回对象的类型。

输入

System.ServiceProcess.ServiceController, System.String

可以将服务对象或包含服务名称的字符串通过管道传递给 Set-Service。

输出

None

此 cmdlet 不返回任何对象。

说明

要在 Windows Vista 以及 Windows 的更高版本上使用 Set-Service,请使用“以管理员身份运行”选项启动 Windows PowerShell。

Set-Service 只有在当前用户有相应权限时才能控制服务。如果某个命令不能正常工作,则可能您不具有所需的权限。

若要查找服务名称并显示您的系统中的服务名称,请键入“get-service”。服务名称显示在 Name 列中,显示名称显示在 DisplayName 列中。

示例 1

C:\PS>set-service -name lanmanworkstation -DisplayName "LanMan Workstation"

说明
-----------
此命令将 lanmanworkstation 服务的显示名称更改为“LanMan Workstation”。(默认值为“Workstation”。)






示例 2

C:\PS>get-wmiobject win32_service -filter "name = 'SysmonLog'"

ExitCode  : 0
Name      : SysmonLog
ProcessId : 0
StartMode : Manual
State     : Stopped
Status    : OK

C:\PS> set-service sysmonlog -startuptype automatic

C:\PS> get-wmiobject win32_service -filter "name = 'SysmonLog'"

ExitCode  : 0
Name      : SysmonLog
ProcessId : 0
StartMode : Auto
State     : Stopped
Status    : OK

C:\PS> get-wmiobject win32_service | format-table Name, StartMode -auto

Name                                  StartMode
----                                  ---------
AdtAgent                              Auto
Alerter                               Disabled
ALG                                   Manual
AppMgmt                               Manual
...

说明
-----------
这些命令获取性能日志和警报 (SysmonLog) 服务的启动类型,将启动模式设置为自动,然后显示更改结果。

这些命令使用 Get-WmiObject cmdlet 获取表示服务的 Win32_Service 对象,因为 Get-Service 所返回的 ServiceController 对象不包含启动模式。

第一条命令使用 Get-WmiObject cmdlet 获取表示 SysmonLog 服务的 Windows Management Instrumentation (WMI) 对象。此命令的默认输出显示该服务的启动类型。

第二条命令使用 Set-Service 将启动模式更改为自动。然后,重复第一条命令以显示更改。

最后一条命令显示计算机上所有服务的启动模式。






示例 3

C:\PS>set-service -name Schedule -computername S1 -description "Configures and schedules tasks."

C:\PS> get-wmiobject win32_service -computername s1 | where-object {$_.Name -eq "Schedule"} | format-list Name, Description

说明
-----------
这些命令更改远程计算机 S1 上的任务计划程序服务的说明,然后显示结果。

这些命令使用 Get-WmiObject cmdlet 获取表示服务的 Win32_Service 对象,因为 Get-Service 所返回的 ServiceController 对象不包含服务说明。

第一条命令使用 Set-Service 命令来更改说明。它使用服务的服务名称“Schedule”来标识该服务。

第二条命令使用 Get-WmiObject cmdlet 来获取表示任务计划程序服务的 WMI Win32_Service 实例。该命令中的第一个元素获取 Win32_service 类的所有实例。

管道运算符 (|) 将结果传递给 Where-Object cmdlet,该 cmdlet 选择 Name 属性中包含“Schedule”值的实例。

另一个管道运算符将结果发送给 Format-List cmdlet,该 cmdlet 将输出的格式设置为只包含 Name 和 Description 属性的列表。






示例 4

C:\PS>set-service winrm -status Running -passthru -computername Server02

说明
-----------
此命令启动计算机 Server02 上的 WinRM 服务。此命令使用 Status 参数指定所需状态(“running”),使用 PassThru 参数指示 Set-Service 返回表示 WinRM 服务的对象。






示例 5

C:\PS>get-service schedule -computername S1, S2 | set-service -status paused

说明
-----------
此命令挂起远程计算机 S1 和 S2 上的计划服务。它使用 Get-Service cmdlet 来获取该服务。管道运算符 (|) 将该服务发送给 Set-Service cmdlet,后者将服务状态更改为“Paused”。






示例 6

C:\PS>$s = get-service schedule

C:\PS> set-service -inputobject $s -status stopped

说明
-----------
这些命令停止本地计算机上的计划服务。

第一条命令使用 Get-Service cmdlet 获取计划服务。此命令将该服务保存在 $s 变量中。

第二条命令使用 Set-Service cmdlet 将计划服务的状态更改为“Stopped”。它使用 InputObject 参数提交 $s 变量中存储的服务,使用 Status 参数指定所需状态。






另请参阅




目录