POLL: POWERSHELL VS. GUI - DO YOU WANT TO BE A DEVOP OR AN ADMIN?
Set service startup mode to Automatic (Delayed Start) with PowerShell
The PowerShell script described here allows you to set the startup mode of a Windows service to Automatic (Delayed Start) on a local computer or on the remote computers specified in a list. The PowerShell script works on Windows 7, Windows 8, Windows 2008 (and R2), and Windows Server 2012 computers.
Automatic (Delayed Start) is a new service startup mode that is available from Windows Vista onward. This startup type allows you to configure less important services to start a bit later when Windows boots up. This reduces the boot time of a Windows computer. Though it is a very good feature, Microsoft didn’t update the Win32_Service WMI class to support this new startup type. Because of this, it is difficult to set this startup type in scripts.
I came up with this script to ease the life of Windows Administrators. The script is a wrapper on top of SC.exe (service controller) that sets a given service to Automatic (Delayed Start). This script takes the service name (NOT the display name of the service) as input through the –ServiceName parameter and sets it to the Automatic (Delayed Start) startup type. You can delay the start of a service on remote computers using the –ComputerName parameter. Both parameters accept multiple values. The –ServiceName parameter is mandatory. The –ComputerName parameter is optional; when this parameter is not specified, it runs against the local computer.
The following code is the core part of the script.
$command = "sc.exe \\$Computer config $Service start= delayed-auto"
$Output = Invoke-Expression -Command $Command -ErrorAction Stop
if($LASTEXITCODE -ne 0){
Write-Host "$Computer : Failed to set $Service to delayed start.
More details: $Output" -foregroundcolor red
$failedcomputers +=$ComputerName
} else {
Write-Host "$Computer : Successfully changed $Service service
to delayed start" -foregroundcolor green
$successcomputers +=$ComputerName
}
This code uses the Invoke-Expression cmdlet to trigger the SC.exe-based command. After execution, it checks the value of $LASTEXITCODE to verify if the command executed successfully. $LASTEXITCODE is a PowerShell built-in variable that contains the return code of the last win32 executable execution. See my blog article to know more about this special variable.
Apart from displaying the output on the PowerShell console, this script saves the details of failed (failedcomputers.txt), success (successcomputers.txt), and unreachable (unreachableComputers.txt) computer names to text files in C:\. The location of these files can be changed by editing the path in the end{} block within the script.
Help and usage instructions:
You can use the Get-Help cmdlet to see the help and usage instructions of this script.
PS C:\scripts> Get-Help .\Set-ServiceDelayedStart.ps1 -Full NAME C:\scripts\Set-ServiceDelayedStart.ps1 SYNOPSIS Sets startup mode of service to "Automatic (Delayed Start)" SYNTAX C:\scripts\Set-ServiceDelayedStart.ps1 [-ServiceName] <String[]> [[-ComputerName] <String[]>] [<CommonParameters>] DESCRIPTION This script helps you set the startup type of a Windows service to "Automatic (Delayed Start)" on local or remote computer(s). PARAMETERS -ServiceName<String[]> ServiceName (not the display name) of the services that you want to set to "Automatic (Delayed Start)" Required? true Position? 1 Default value Accept pipeline input? false Accept wildcard characters? -ComputerName<String[]> Name of the computer(s) on which you want to set the start type of services to "Automatic (Delayed Start)" Required? false Position? 2 Default value Accept pipeline input? true (ByValue, ByPropertyName) Accept wildcard characters? <CommonParameters> This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type "get-helpabout_commonparameters". INPUTS OUTPUTS NOTES NAME: Set-ServiceDelayedStart.ps1 AUTHOR: Sitaram Pamarthi WEBSITE: http://techibee.com -------------------------- EXAMPLE 1 -------------------------- C:\PS>Set-ServiceDelayedStart.ps1 -ServiceName MYSQL Changes the startup mode of the MYSQL service to "Automatic (Delayed Start)" on the local computer -------------------------- EXAMPLE 2 -------------------------- C:\PS>Set-ServiceDelayedStart.ps1 -ComputerName comp1 -ServiceName MYSQL Changes the startup mode of the MYSQL service to "Automatic (Delayed Start)" on Comp1 -------------------------- EXAMPLE 3 -------------------------- C:\PS>Set-ServiceDelayedStart.ps1 -ComputerName comp1,Comp2 -ServiceNameMYSQL,LiveUpdate Changes the startup type of the MYSQL, LiveUpdate service to "Automatic (Delayed Start)" on Comp1 and Comp1 -------------------------- EXAMPLE 4 -------------------------- C:\PS>Get-Content c:\servers.txt | Set-ServiceDelayedStart.ps1 -ServiceName MYSQL Changes the startup type of the MYSQL, LiveUpdate service to "Automatic (Delayed Start)" on Comp1 and Comp1 RELATED LINKS PS C:\scripts>
You can download the PowerShell script here. Feel free to ask questions in a comment below.
By
Hey ,
Why wouldnt you use native powershell ?
Set-Service -ComputerName -Name -StartupType Delayed
I think it would do the job as well
Regards
Rafal
Hi Rafal, Sorry for the delayed reply. There is no startup type called “Delayed” for Set-Service cmdlet. Have you tried it? I am sure it will fail. See http://technet.microsoft.com/en-us/library/hh849849.aspx for details.