- Add a domain user or group to local administrators with PowerShell - Wed, Mar 19 2014
- Create a list of local administrators with PowerShell - Wed, Mar 5 2014
- Remotely query user profile information with PowerShell - Tue, Nov 26 2013
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.
Subscribe to 4sysops newsletter!
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.
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.
Hello All,
I wanted to say that this script has been fantasic. I was able to change most of the services I needed. I ran into an issue though with services that had a $ in them. For example, BTSSvc$BizTalkServerApplication from BizTalk 2013. Is there a way I can make it like the name I’m using.
Matthew you can use single-quotes, like
‘BTSSvc$BizTalkServerApplication’
That worked for med 🙂
Great post, helped me a lot today at work.
Great post. Thank you.
There are other options in a similar post http://metah.ch/blog/2012/09/how-to-change-a-service-startup-type-with-powershell/.
Script failing if service name contains spaces such as “Citrix Encryption Service”. What to do?
Error:
Failed to set CITRIX ENCRYPTION SERVICE to delayed start. More details: DESCRIPTION: Modifies
a service entry in the registry and Service Database. USAGE: sc <server> config [service name] <option1> <option
2>… OPTIONS: NOTE: The option name includes the equal sign. A space is required between the equal sign and the
value. type= <own|share|interact|kernel|filesys|rec|adapt> start= <boot|system|auto|demand|disabled|delayed-auto> err
or= <normal|severe|critical|ignore> binPath= <BinaryPathName> group= <LoadOrderGroup> tag= <yes|no> depend= <Depende
ncies(separated by / (forward slash))> obj= <AccountName|ObjectName> DisplayName= <display name> password= <password>
For services there is a Display Name(with spaces usually) property and also just a ‘Name’ property, that doesn’t have any spaces. If you double click on a service you will see the name. That’s what you want to use for the script not the display name..
To get Automatic (Delayed start) using Powershell, you need to use “Start” and the number 2:
$ComputerName = Read-Host “Enter Computer name”
Set-Service -ComputerName $ComputerName -Name Themes -Start 2
—–
Startup Type: Automatic = 2 (Delayed)
Startup Type: Manual = 3
Startup Type: Disabled = 4
—–
Get-Item -Path ‘HKLM:\SYSTEM\CurrentControlSet\Services\Themes’
Result:
Start = 2
DelayedAutostart = 1
—–
Hi, I actually have a service that the name and service name both have spaces in them. I ran the script making sure to put quotes around the service name, but still got the same error as Nikhil above.
i get
Failed to set $Service to delayed start.
more datils: SC openSCmanager failed 6: The handel is invalid.
what am i doing wrong?
I need script to check and start all the stopped services which are set auto after patched and rebooted. there will be multiple servers. Kindly help if any one having it.
Hi Sitaram,
My requirement is to start services one by one.Start service "1" and wait until it's status is Started then start 2nd service and so on.
Is there any powershell script to do this?
Thanks
2 things, since this is an old topic.
1. An Automatic Delayed start type for a service consists of 2 values in the registry key:
Startup = 2 (Automatic)
DelayedAutoStart = 1
The DelayedAutoStart is ignored unless the Startup is set to 2
Sirisha:
You can set a registry key to create the dependency:
https://www.c-sharpcorner.com/UploadFile/0f68f2/adding-service-dependency-on-a-windows-service-through-regis/
Basically:
If that's not going to cut it, then you could go the route:
Using a for/foreach loop, you could do the same thing as above..
David F.
Hi Sitaram / Tech members,
Question – in servers.txt do we have to mention only the server name(computer name) and
I have multiple services of my application on windows servers, How can we achieve that?