Setting a Startup type of Service with Powershell

Recently I needed to change the Start Type of a Windows Service from Powershell. Course i need to know which ones are disable out of my Filtered set of Services.

CmdLet 'Get-Service'

it produces an Object ofSystem.ServiceProcess.ServiceControllerObject which doesn't have Startuptype parameter. This was primaryproblem on how to query the services which have startup type as disabled, and then setting the startup type of the services to whatever I intend to set.

Doing some binging i also saw some P/Invoke based C# code which does need a thought to code and compile. I wanted the solution to be simple and which works on most of the Windows systems without the need to write code, compile and port.

  • Powershell offers a easy interface to CIM Instances for getting Service Objects out of CIM implementation. The command below just returns the service objects which are disabled

[code language="powershell"] Get-CimInstance Win32_Service -filter "startmode='disabled'" | get-member Output Object type is "Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Service" ```

  • This is very much true with regards to WMI as well, though the Outputobject is a bit different but has most of the parameters/ methods as CIMInstance object.

[code language="powershell"] Get-WmiObject Win32_Service -filter "startmode='disabled'" | get-member Output Object type is System.Management.ManagementObject#root\cimv2\Win32_Service ```

Pipe-lining to Set-service

CmdLet Set-service has 2 type of implementations which take pipeline inputs as either "System.ServiceProcess.ServiceController" or System.String which -Name value.

--InputObject-(ByValue)

--Name-(ByValue, ByPropertyName)

Clubbing it together .

Looking at the Output-object of Get-CIMInstance/Get-WmiObject for win32_service revealed that 'Name' property has the value of actual name of the Service fitting in my filter criteria. The Name parameter matches with the ByPropertyName based -Name pipeline input to the Set-Service CmdLet .

Hence, below commands work a good way.

1. List of Services having Disabled as Startup type. Remember the property is startmode rather than Startuptype .

a.Get-CimInstance Way . Both of the below commands produce same output : -

[code language="powershell"] Get-CimInstanceWin32_service -filter "startmode='disabled' and Name like '%SQL%'" Get-CimInstance Win32_Service | where {$_.startmode -eq 'Disabled' -and $_.name -like '*sql*'} ProcessId Name StartMode State Status ExitCode --------- ---- --------- ----- ------ -------- 0 SQLBrowser Disabled Stopped OK 1077

0 SQLAgent$SHAR... Disabled Stopped OK 1077 ``` b. WMI implementation of above commands below

[code language="powershell"] Get-WmiObject Win32_service -filter "startmode='disabled' and Name like '%SQL%'" Get-WmiObject Win32_Service | where {$_.startmode -eq 'Disabled' -and $_.name -like'*sql*'} ```

2. Setting the Service startup type now

Below command sets this up perfectly. Watch out for -whatif output before running this to see what you are doing.

[code language="powershell"] Get-CimInstance win32_service -filter "startmode='disabled' and Name like '%SQL%'" | set-service -StartupType Disabled -whatif What if: Performing the operation "Set-Service" on target "SQL Server Browser (SQLBrowser)". What if: Performing the operation "Set-Service" on target "SQL Server Agent (SHAREPOINT) (SQLAgent$SHAREPOINT)".

Get-CimInstance win32_service -filter "startmode='disabled' and Name like '%SQL%'" | set-service -StartupType Manual ```

I hope this helps you.