共用方式為


Set-Service

啟動、停止和暫停服務,並變更其屬性。

語法

Set-Service
   [-Name] <String>
   [-DisplayName <String>]
   [-Credential <PSCredential>]
   [-Description <String>]
   [-StartupType <ServiceStartupType>]
   [-Status <String>]
   [-SecurityDescriptorSddl <String>]
   [-Force]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
Set-Service
   [-InputObject] <ServiceController>
   [-DisplayName <String>]
   [-Credential <PSCredential>]
   [-Description <String>]
   [-StartupType <ServiceStartupType>]
   [-SecurityDescriptorSddl <String>]
   [-Status <String>]
   [-Force]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

Description

此 Cmdlet 只能在 Windows 平臺上使用。

Set-Service Cmdlet 會變更服務的屬性,例如 StatusDescriptionDisplayNameStartupTypeSet-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 參數會將服務設定為 Automatic

Get-Service 會使用 Name 參數來指定 BITS 服務,並將物件傳送至管線。 Select-Object 會使用 Property 參數來顯示 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 參數來指定 Schedule 服務,並將物件傳送至管線。 Set-Service 使用 Status 參數,將服務設定為 Paused

範例 6:停止服務

此範例會使用變數來停止服務。

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

Get-Service 使用 Name 參數來指定服務,Schedule。 物件會儲存在 變數中,$SSet-Service 會使用 InputObject 參數,並指定儲存 $S的物件。 Status 參數會將服務設定為 已停止

範例 7:停止遠端系統上的服務

此範例會停止遠端電腦上的服務。 如需詳細資訊,請參閱 Invoke-Command

$Cred = Get-Credential
$S = Get-Service -Name Schedule
Invoke-Command -ComputerName server01.contoso.com -Credential $Cred -ScriptBlock {
  Set-Service -InputObject $S -Status Stopped
}

Get-Credential 提示輸入使用者名稱和密碼,並將認證儲存在 $Cred 變數中。 Get-Service 會使用 Name 參數來指定 Schedule 服務。 物件會儲存在 變數中,$S

Invoke-Command 會使用 ComputerName 參數來指定遠端電腦。 Credential 參數會使用 $Cred 變數登入計算機。 ScriptBlock 會呼叫 Set-ServiceInputObject 參數會指定儲存在 $S的服務物件。 Status 參數會將服務設定為 已停止

範例 8:變更服務的認證

此範例會變更用來管理服務的認證。

$credential = Get-Credential
Set-Service -Name Schedule -Credential $credential

Get-Credential 提示輸入使用者名稱和密碼,並將認證儲存在 $credential 變數中。 Set-Service 會使用 Name 參數來指定 Schedule 服務。 Credential 參數會使用 $credential 變數,並更新 Schedule 服務。

範例 9:變更服務的 SecurityDescriptor

此範例會變更服務的 SecurityDescriptor

$SDDL = "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;SU)"
Set-Service -Name "BITS" -SecurityDescriptorSddl $SDDL

SecurityDescriptor 會儲存在 $SDDL 變數中。 Set-Service 會使用 Name 參數來指定 BITS 服務。 SecurityDescriptorSddl 參數會使用 $SDDL 來變更 BITS 服務的 SecurityDescriptor

參數

-Confirm

在執行 Set-Service之前,提示您進行確認。

類型:SwitchParameter
別名:cf
Position:Named
預設值:False
必要:False
接受管線輸入:False
接受萬用字元:False

-Credential

指定服務使用的帳戶做為 服務登入帳戶

輸入用戶名稱,例如 User01Domain01\User01,或輸入 PSCredential 物件,例如 Get-Credential Cmdlet 所產生的用戶名稱。 如果您輸入使用者名稱,此 Cmdlet 會提示您輸入密碼。

認證會儲存在 PSCredential 物件中,密碼會儲存為 secureString

注意

如需 SecureString 數據保護的詳細資訊,請參閱 SecureString 有多安全?

此參數是在 PowerShell 6.0 中引進的。

類型:PSCredential
Position:Named
預設值:None
必要: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

-Force

指定服務的停止模式。 此參數僅適用於使用 -Status Stopped 時。 如果啟用,Set-Service 會在目標服務停止之前停止相依服務。 根據預設,當其他執行中的服務相依於目標服務時,就會引發例外狀況。

類型:SwitchParameter
Position:Named
預設值:False
必要:False
接受管線輸入:False
接受萬用字元:False

-InputObject

指定代表要變更之服務的 ServiceController 物件。 輸入包含 物件的變數,或輸入取得物件的命令或表達式,例如 Get-Service 命令。 您可以使用管線將服務物件傳送至 Set-Service

類型:ServiceController
Position:0
預設值:None
必要:True
接受管線輸入: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

-SecurityDescriptorSddl

Sddl 格式指定服務的 SecurityDescriptor

類型:String
別名:sd
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-StartupType

指定服務的啟動模式。

此參數可接受的值如下:

  • 自動 - 服務是在系統啟動時由作業系統啟動或啟動。 如果自動啟動的服務相依於手動啟動的服務,系統啟動時也會自動啟動手動啟動服務。
  • AutomaticDelayedStart - 在系統開機后不久啟動。
  • 停用 - 服務已停用,且無法由使用者或應用程式啟動。
  • InvalidValue - 沒有作用。 Cmdlet 不會傳回錯誤,但服務的 StartupType 不會變更。
  • 手動 - 服務只會由使用者使用服務控制管理員或應用程式手動啟動。
類型:ServiceStartupType
別名:StartMode, SM, ST, StartType
接受的值:Automatic, AutomaticDelayedStart, Disabled, InvalidValue, Manual
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 物件。

備註

此 Cmdlet 僅適用於 Windows 平臺。

Set-Service 需要提高的許可權。 使用 [以系統管理員身分執行 選項。

Set-Service 只有在目前使用者具有管理服務的許可權時,才能控制服務。 如果命令無法正確運作,您可能沒有必要的許可權。

若要尋找服務的服務名稱或顯示名稱,請使用 Get-Service。 服務名稱位於 [Name] 數據行中,而顯示名稱位於 DisplayName 數據行中。