共用方式為


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>]

Description

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 參數會將服務設定為 已停止

參數

-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。 服務名稱位於 [Name] 數據行中,而顯示名稱位於 DisplayName 數據行中。