Built-in Resources
PowerShell Desired State Configuration (DSC) provides a rich set of built-in resources to manage system configurations. These resources abstract common administrative tasks into reusable components, enabling consistent and declarative management of resources like files, registry keys, services, and more. Understanding these resources is essential for building robust DSC configurations that enforce desired states across Windows systems.
File Resource¶
The File resource ensures a file or directory exists, is absent, or matches specific content. It is ideal for managing configuration files, scripts, or data files.
Key Parameters¶
- Ensure:
Present(default) orAbsentto define the desired state. - Path: Full path to the file or directory.
- Contents: String content to write to the file (used with
Ensure = Present). - Force:
$trueto overwrite existing files.
Example: Create a configuration file¶
File 'ExampleFile' {
Ensure = 'Present'
Path = 'C:\Example\config.txt'
Contents = 'This is a test file.'
Force = $true
}
Registry Resource¶
The Registry resource manages registry keys and values. It is useful for configuring system settings, application preferences, or security policies.
Key Parameters¶
- Ensure:
Present(default) orAbsent. - Key: Full registry key path (e.g.,
HKLM:\Software\Example). - ValueName: Name of the registry value.
- ValueData: Data to set (string or binary).
- Type: Registry value type (e.g.,
String,DWord,MultiString).
Example: Set a registry value¶
Registry 'ExampleRegistryKey' {
Ensure = 'Present'
Key = 'HKLM:\Software\ExampleApp'
ValueName = 'LogLevel'
ValueData = 'Verbose'
Type = 'String'
}
Service Resource¶
The Service resource manages Windows services, ensuring they are running, stopped, or configured with specific properties.
Key Parameters¶
- Ensure:
Present(default) orAbsent. - Name: Service name (e.g.,
WindowsEventLog). - State:
Running(default) orStopped. - StartupType:
Automatic,Manual, orDisabled.
Example: Configure a service¶
Service 'ExampleService' {
Ensure = 'Present'
Name = 'w3svc'
State = 'Running'
StartupType = 'Automatic'
}
Additional Built-in Resources¶
Other commonly used resources include:
- Package: Installs software (e.g., MSI, EXE).
- WindowsFeature: Manages Windows Server roles/features.
- User: Creates or modifies user accounts.
- Group: Manages local or domain groups.
These resources often require additional parameters (e.g., SourcePath for Package) and may have version-specific behavior. Always verify compatibility with your PowerShell/DSC version.
Key takeaways¶
- The
File,Registry, andServiceresources are foundational for managing files, registry settings, and services. - Each resource uses
Ensureto define the desired state and supports dependencies viaDependsOn. - Combine resources to enforce complex configurations, such as creating a file, setting a registry key, and starting a service in a single configuration.
- Use built-in resources to avoid writing custom scripts, ensuring consistency and auditability in DSC configurations.