Reputation: 899
During maintenance, before I stop a Windows service, I need set its start type to Manual. Later I need switch it back to its original start type. So I need know the start type before I stop a service.
In Windows 10, I know there is a property called "DelayedAutoStart", but it seems not available in Windows Server 2012. How can I get the start type of a service in Powershell?
I am using Powershell 5.1 on Windows Server 2012.
Upvotes: 2
Views: 9087
Reputation: 332
The "Clean Powershell 5.1" method is to query the registry path. Microsoft, in its endless wisdom, missed that tiny detail when they created the Get-Service cmdlet. This will query all services, check the delayed Autostart and output the list (this example limits to one service).
$Services = Get-Service | Select-Object *,DelayedAutoStart
for ($i = 0 ; $i -lt $Services.Count ; $i++ ) {
$Services[$i].DelayedAutoStart = (Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\$($Services[$i].Name)").DelayedAutoStart
}
$Services.Where({$_.Name -eq "DispBrokerDesktopSvc"}) | ft Name,StartType,DelayedAutoStart
Name StartType DelayedAutoStart
---- --------- ----------------
DispBrokerDesktopSvc Automatic 1
Explanation: If the DelayedAutoStart is set to 1 it is delayed. If set to 0 not if is does not exist it is not delayed. If you use Set-Service to change the startup type to disabled the "Delayed Startup" flag won't be changed!
Set-Service -Name DispBrokerDesktopSvc -StartupType Disabled
Press F5 services.msc, and it is disabled.
Set-Service -Name DispBrokerDesktopSvc -StartupType Automatic
Press F5 in services.msc, it is enabled again with delayed startup type as it was before. If you change the "DelayedAutoStart" registry key the change won't be reflected until services.exe process is restarted, which means until the computer is restarted. You have to go back to SC.EXE if you want it reflected immediately. If you add "DelayedAutoStart" registry to a service which does not yet have that value don't count on it working, the service itself has to support that config or it will be ignored.
Upvotes: 0
Reputation: 1388
Here is a good post with a few approaches to handle the DelayedAutoStart
property of a Windows service.
For your version of PowerShell, you're best off utilizing sc.exe.
You can query for a services start type using sc.exe
but the information is returned as text, not PowerShell objects so you have to do some text manipulation. I hacked together a quick one-liner that can get the start type for a service given a name.
sc.exe qc "SERVICE_NAME" | Select-String "START_TYPE" | ForEach-Object { ($_ -replace '\s+', ' ').trim().Split(" ") | Select-Object -Last 1 }
Here is an example where I utilize it in conjunction with a loop to get the state of every service on the machine.
foreach($Service in (Get-Service)) {
Write-Host "$($Service.ServiceName)"
sc.exe qc "$($Service.ServiceName)" | Select-String "START_TYPE" | ForEach-Object { ($_ -replace '\s+', ' ').trim().Split(" ") | Select-Object -Last 1 }
}
You can set the start type of a service doing something similar to the following...
sc.exe config NameOfTheService start= delayed-auto
or wrapping sc.exe
in PowerShell...
$myArgs = 'config "{0}" start=delayed-auto' -f 'TheServiceName'
Start-Process -FilePath sc.exe -ArgumentList $myArgs
As of PowerShell 6.0, they've added the support for AutomaticDelayedStart
, however since you're using PowerShell 5.1 this doesn't apply (but it may for other readers).
Set-Service -Name "Testservice" –StartupType "AutomaticDelayedStart"
Upvotes: 4