Set Service - PowerShell
Overview
The Set-Service
cmdlet in PowerShell allows you to configure various aspects of a Windows service. It enables you to modify service properties such as the startup type, recovery actions, and dependencies. This command is crucial for managing services, ensuring optimal system performance, troubleshooting, and automating service-related tasks.
Syntax
Set-Service [-Name] <ServiceName> [-DisplayName] <DisplayName> [-StartupType] <StartupType> [-Status] <ServiceStatus> [-RecoveryAction] <RecoveryAction> [-RecoveryTimeout] <RecoveryTimeout> [-FailureActions] <FailureActions> [-FailureTimeout] <FailureTimeout> [-Priority] <Priority> [-DelayedAutoStart] <DelayedAutoStart> [-ServiceFlags] <ServiceFlags> [-Password] <SecureString> [-Confirm] [-WhatIf] [-PassThru]
Options/Flags
- -Name: Specifies the name of the service to be configured.
- -DisplayName: Sets the display name for the service.
- -StartupType: Configures the service’s startup type. Options include:
- Automatic
- Manual
- Disabled
- AutomaticDelayedStart
- -Status: Specifies the current status of the service. Options include:
- Running
- Stopped
- Paused
- -RecoveryAction: Defines the action to be taken if a service fails. Options include:
- Restart
- RunCommand
- RestartComputer
- -RecoveryTimeout: Sets the time (in milliseconds) before the recovery action is initiated.
- -FailureActions: Configures multiple actions to be taken upon service failure.
- -FailureTimeout: Specifies the time (in milliseconds) before the failure action is initiated.
- -Priority: Sets the priority of the service. Higher priority services are started first.
- -DelayedAutoStart: Delays the automatic startup of the service after system boot.
- -ServiceFlags: Configures additional service flags. Options include:
- Interactive
- InteractiveProcess
- OwnProcess
- SharedProcess
- -Password: Sets the service password if required.
- -Confirm: Prompts for confirmation before executing the command.
- -WhatIf: Simulates the execution of the command without making any changes.
- -PassThru: Returns the updated service object.
Examples
Example 1: Set Service Display Name
Set-Service -Name "MyService" -DisplayName "My Custom Service"
Example 2: Change Service Startup Type
Set-Service -Name "MyService" -StartupType Automatic
Example 3: Configure Recovery Action and Timeout
Set-Service -Name "MyService" -RecoveryAction Restart -RecoveryTimeout 60000
Example 4: Set Service Priority and Delay Startup
Set-Service -Name "MyService" -Priority High -DelayedAutoStart $true
Common Issues
Issue: Permission denied error when setting service properties.
Solution: Ensure that you are running PowerShell with elevated privileges (as an administrator).
Integration
Set-Service
can be combined with other PowerShell commands for advanced automation tasks. For example:
Script to Start and Set Recovery Action for Multiple Services
$services = Get-Service -Name "Service1", "Service2"
Set-Service -InputObject $services -StartupType Automatic -RecoveryAction Restart
Related Commands
Get-Service
: Retrieves information about Windows services.New-Service
: Creates a new service.Restart-Service
: Restarts a stopped service.Stop-Service
: Stops a running service.- Microsoft documentation on Set-Service