|
| 1 | +--- |
| 2 | +description: > |
| 3 | + Example showing how to use the Microsoft.Windows/Service resource in a DSC configuration |
| 4 | + document to enforce the desired state of Windows services. |
| 5 | +ms.date: 05/08/2026 |
| 6 | +ms.topic: reference |
| 7 | +title: Configure a Windows service |
| 8 | +--- |
| 9 | + |
| 10 | +# Configure a Windows service |
| 11 | + |
| 12 | +This example shows how you can use the `Microsoft.Windows/Service` resource in a DSC configuration |
| 13 | +document to enforce the desired configuration and runtime status of multiple Windows services. |
| 14 | + |
| 15 | +> [!IMPORTANT] |
| 16 | +> **Set** operations for this resource require an elevated (administrator) process context. Run |
| 17 | +> your terminal or PowerShell session as Administrator before using `dsc config set`. |
| 18 | +
|
| 19 | +## Definition |
| 20 | + |
| 21 | +The configuration document for this example defines two instances of the `Service` resource. |
| 22 | + |
| 23 | +The first instance ensures that the Print Spooler service (`Spooler`) is stopped and configured |
| 24 | +for manual start. The second instance ensures that the Windows Time service (`W32Time`) is running |
| 25 | +and configured to start automatically. |
| 26 | + |
| 27 | +:::code language="yaml" source="service.config.dsc.yaml"::: |
| 28 | + |
| 29 | +Copy the configuration document and save it as `service.config.dsc.yaml`. |
| 30 | + |
| 31 | +## Setup |
| 32 | + |
| 33 | +The output in this example assumes that the system has the `Spooler` service stopped with a manual |
| 34 | +startup and the `W32Time` service stopped with an automatic startup. You can set the system to |
| 35 | +this starting state with the following commands: |
| 36 | + |
| 37 | +```powershell |
| 38 | +Set-Service -Name Spooler -StartupType Manual -Status Stopped |
| 39 | +Set-Service -Name W32Time -StartupType Automatic -Status Stopped |
| 40 | +``` |
| 41 | + |
| 42 | +## Test the configuration |
| 43 | + |
| 44 | +To see whether the system is already in the desired state, use the [dsc config test][01] command. |
| 45 | + |
| 46 | +```powershell |
| 47 | +dsc config test --file ./service.config.dsc.yaml |
| 48 | +``` |
| 49 | + |
| 50 | +```yaml |
| 51 | +executionInformation: |
| 52 | + # Elided for brevity |
| 53 | +metadata: |
| 54 | + # Elided for brevity |
| 55 | +results: |
| 56 | +- executionInformation: |
| 57 | + duration: PT0.1113118S |
| 58 | + metadata: |
| 59 | + Microsoft.DSC: |
| 60 | + duration: PT0.1113118S |
| 61 | + name: Ensure Print Spooler is stopped and set to manual start |
| 62 | + type: Microsoft.Windows/Service |
| 63 | + result: |
| 64 | + desiredState: |
| 65 | + name: Spooler |
| 66 | + status: Stopped |
| 67 | + startType: Manual |
| 68 | + actualState: |
| 69 | + name: Spooler |
| 70 | + displayName: Print Spooler |
| 71 | + description: This service spools print jobs and handles interaction with the printer. If you turn off this service, you won't be able to print or see your printers. |
| 72 | + _exist: true |
| 73 | + status: Stopped |
| 74 | + startType: Manual |
| 75 | + executablePath: C:\Windows\System32\spoolsv.exe |
| 76 | + logonAccount: LocalSystem |
| 77 | + errorControl: Normal |
| 78 | + dependencies: |
| 79 | + - RPCSS |
| 80 | + inDesiredState: true |
| 81 | + differingProperties: [] |
| 82 | +- executionInformation: |
| 83 | + duration: PT0.0353328S |
| 84 | + metadata: |
| 85 | + Microsoft.DSC: |
| 86 | + duration: PT0.0353328S |
| 87 | + name: Ensure Windows Time service is running |
| 88 | + type: Microsoft.Windows/Service |
| 89 | + result: |
| 90 | + desiredState: |
| 91 | + name: W32Time |
| 92 | + status: Running |
| 93 | + startType: Automatic |
| 94 | + actualState: |
| 95 | + name: W32Time |
| 96 | + displayName: Windows Time |
| 97 | + description: Maintains date and time synchronization on all clients and servers in the network. If this service is stopped, date and time synchronization will be unavailable. If this service is disabled, any services that explicitly depend on it will fail to start. |
| 98 | + _exist: true |
| 99 | + status: Stopped |
| 100 | + startType: Automatic |
| 101 | + executablePath: C:\Windows\system32\svchost.exe -k LocalService |
| 102 | + logonAccount: NT AUTHORITY\LocalService |
| 103 | + errorControl: Normal |
| 104 | + inDesiredState: false |
| 105 | + differingProperties: |
| 106 | + - status |
| 107 | +messages: [] |
| 108 | +hadErrors: false |
| 109 | +``` |
| 110 | +
|
| 111 | +The `inDesiredState` field for the first instance is `true` because the Print Spooler service is |
| 112 | +already `Stopped` with `Manual` start, so no change is required. The second instance is `false`: |
| 113 | +the Windows Time service exists and already has `startType: Automatic`, but its `status` is |
| 114 | +`Stopped` while the desired state requires `Running`. Only `status` is listed in |
| 115 | +`differingProperties`. |
| 116 | + |
| 117 | +## Set the configuration |
| 118 | + |
| 119 | +To enforce the desired state, use the [dsc config set][02] command. |
| 120 | + |
| 121 | +```powershell |
| 122 | +dsc config set --file ./service.config.dsc.yaml |
| 123 | +``` |
| 124 | + |
| 125 | +```yaml |
| 126 | +executionInformation: |
| 127 | + # Elided for brevity |
| 128 | +metadata: |
| 129 | + # Elided for brevity |
| 130 | +results: |
| 131 | +- executionInformation: |
| 132 | + duration: PT0.0924309S |
| 133 | + metadata: |
| 134 | + Microsoft.DSC: |
| 135 | + duration: PT0.0924309S |
| 136 | + name: Ensure Print Spooler is stopped and set to manual start |
| 137 | + type: Microsoft.Windows/Service |
| 138 | + result: |
| 139 | + beforeState: |
| 140 | + name: Spooler |
| 141 | + status: Stopped |
| 142 | + startType: Manual |
| 143 | + afterState: |
| 144 | + name: Spooler |
| 145 | + displayName: Print Spooler |
| 146 | + description: This service spools print jobs and handles interaction with the printer. If you turn off this service, you won't be able to print or see your printers. |
| 147 | + _exist: true |
| 148 | + status: Stopped |
| 149 | + startType: Manual |
| 150 | + executablePath: C:\Windows\System32\spoolsv.exe |
| 151 | + logonAccount: LocalSystem |
| 152 | + errorControl: Normal |
| 153 | + dependencies: |
| 154 | + - RPCSS |
| 155 | + changedProperties: null |
| 156 | +- executionInformation: |
| 157 | + duration: PT0.3682548S |
| 158 | + metadata: |
| 159 | + Microsoft.DSC: |
| 160 | + duration: PT0.3682548S |
| 161 | + name: Ensure Windows Time service is running |
| 162 | + type: Microsoft.Windows/Service |
| 163 | + result: |
| 164 | + beforeState: |
| 165 | + name: W32Time |
| 166 | + displayName: Windows Time |
| 167 | + description: Maintains date and time synchronization on all clients and servers in the network. If this service is stopped, date and time synchronization will be unavailable. If this service is disabled, any services that explicitly depend on it will fail to start. |
| 168 | + _exist: true |
| 169 | + status: Stopped |
| 170 | + startType: Automatic |
| 171 | + executablePath: C:\Windows\system32\svchost.exe -k LocalService |
| 172 | + logonAccount: NT AUTHORITY\LocalService |
| 173 | + errorControl: Normal |
| 174 | + afterState: |
| 175 | + name: W32Time |
| 176 | + displayName: Windows Time |
| 177 | + description: Maintains date and time synchronization on all clients and servers in the network. If this service is stopped, date and time synchronization will be unavailable. If this service is disabled, any services that explicitly depend on it will fail to start. |
| 178 | + _exist: true |
| 179 | + status: Running |
| 180 | + startType: Automatic |
| 181 | + executablePath: C:\Windows\system32\svchost.exe -k LocalService |
| 182 | + logonAccount: NT AUTHORITY\LocalService |
| 183 | + errorControl: Normal |
| 184 | + changedProperties: |
| 185 | + - status |
| 186 | +messages: [] |
| 187 | +hadErrors: false |
| 188 | +``` |
| 189 | + |
| 190 | +The Print Spooler instance shows `changedProperties: null` because it was already in the desired |
| 191 | +state and DSC made no changes to it. The Windows Time instance lists only `status` in |
| 192 | +`changedProperties` because DSC only needed to start the service. The `startType` was already |
| 193 | +`Automatic` and required no update. |
| 194 | + |
| 195 | +<!-- Link definitions --> |
| 196 | +[01]: ../../../../../cli/config/test.md |
| 197 | +[02]: ../../../../../cli/config/set.md |
0 commit comments