2

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.

Jim
  • 769
  • 3
  • 10
  • 22

2 Answers2

4

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.

Querying service start type

You can query for a services start type using sc.exebut 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 } 
}

Setting service start type

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"
Zam
  • 1,121
  • 10
  • 27
  • 1
    Thanks a lot for the answre. Is it possible to use wildcard in sc.exe to query? or is it possible to pipe the service name to sc.exe, for example use get-service to get the list of services, then pass it to sc.exe? – Jim Dec 10 '19 at 07:24
  • Hi Jim, Yes I think the best approach is to use the `Get-Service` cmdlet and then loop over the services that it returns. `Get-Service` returns an object with a property `ServiceName` which is the same name that you need to use when querying with sc.exe. I'll update my answer with an example that gets the start type of every service. From there you can apply a filter to the `Get-Service` cmdlet to narrow it down if you need to. – Zam Dec 10 '19 at 13:42
0

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.

Joachim Otahal
  • 272
  • 2
  • 9