Ensure a Windows Service Runs Automatically Using PowerShell DSC
Step‑by‑step guide to declare, apply, and verify a DSC configuration that guarantees a service exists, is set to start automatically, and is running.
24 Dec 2025, 14:19 UTC

Desired outcome
After the DSC configuration is applied, the target Windows service (for example, the Print Spooler service named Spooler) will:
- Exist on the system.
- Have its
StartTypeset toAutomatic. - Be in the
Runningstate.
Prerequisites
- PowerShell 5.1 or later (built‑in on Windows 10/11 and Windows Server 2016+).
- The
PSDesiredStateConfigurationmodule (included with Windows Management Framework 5.1+). - Administrator privileges on the target node.
- If managing a remote computer, WinRM must be enabled and firewall rules must allow PowerShell remoting.
Focused procedure
- Create the DSC configuration file
Save the following script as
EnsureSpooler.ps1on a management workstation.Configuration EnsureSpoolerService { Node 'localhost' { Service SpoolerService { Name = 'Spooler' StartupType = 'Automatic' State = 'Running' DependsOn = '[Service]SpoolerService' # self‑reference to avoid duplicate block warnings } } }Run the script to define the configuration:
# Run in an elevated PowerShell session .\EnsureSpooler.ps1 - Compile the configuration to a MOF file
This step creates the
MOFthat the Local Configuration Manager (LCM) consumes.EnsureSpoolerService -OutputPath ./OutputThe MOF will be placed in
./Output/EnsureSpoolerService.mof. - Apply the configuration
For a local target, use
Start-DscConfiguration. For a remote target, create a CIM session first.# Local application (requires admin) Start-DscConfiguration -Path ./Output -Wait -Verbose -Force # Remote application example (replace COMPUTER01 with the target name) # $session = New-CimSession -ComputerName COMPUTER01 -Credential (Get-Credential) # Start-DscConfiguration -Path ./Output -CimSession $session -Wait -Verbose -ForceThe
-Waitflag makes the command block until LCM finishes;-Verboseprovides detailed progress.
Expected checks
- Validate service state directly
Get-Service -Name Spooler | Select-Object Name, StartType, StatusExpected output:
Name StartType Status ---- --------- ------ Spooler Automatic Running - Run DSC compliance test
Test-DscConfigurationA return value of
Trueindicates the node matches the applied MOF. - Review DSC operational logs (optional)
Get-WinEvent -LogName Microsoft-Windows-DSC/Operational | Where-Object TimeCreated -gt (Get-Date).AddHours(-1) | Format-Table TimeCreated, Message -AutoSizeLook for events with IDs 4100 (configuration applied) or 4104 (errors).
Recovery options
If the configuration fails to apply or the service drifts from the desired state:
- Re‑apply the same MOF with force
Start-DscConfiguration -Path ./Output -Wait -Verbose -ForceThis tells LCM to enforce the configuration again, overwriting any conflicting settings.
- Roll back to the last known good configuration
DSC retains a history of applied MOFs under
$env:SystemRoot\System32\Configuration. To revert:# List available configurations Get-DscConfigurationStatus -All # Choose a previous ConfigurationID (e.g., GUID) and restore Start-DscConfiguration -Path $env:SystemRoot\System32\Configuration\ -Wait -Force - Manual service reset (as a last resort)
Set-Service -Name Spooler -StartupType Automatic Start-Service -Name SpoolerAfter manual correction, re‑run
Test-DscConfigurationto confirm LCM now reports compliance.
Limitations and verification tips
- The
Service block cannot be duplicated for the same service name within a single configuration; duplicates cause a compilation error. - LCM must be in a mode that allows
ApplyAndMonitororApplyAndAutoCorrect(the default). If LCM is set toApplyOnly, the configuration will be applied once but will not auto‑correct drift. - When targeting remote machines, ensure WinRM listener is configured (
winrm quickconfig) and that the firewall allows inbound traffic on ports 5985 (HTTP) or 5986 (HTTPS). - Always run verification steps (
Get-Service,Test-DscConfiguration) after applying or rolling back to confirm the desired state is achieved.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.