Configure keys and values with Microsoft.Windows/Registry

This example shows how you can use the Microsoft.Windows/Registry resource to manage several registry keys and values in a DSC Configuration Document.

Important

The registry command and Microsoft.Windows/Registry resource are a proof-of-concept example for use with DSCv3. Don't use it in production.

Definition

The configuration document defines three instances of the resource:

  • The instance named Tailspin Key ensures that the key tailspin exists in the current user hive.
  • The instance named Tailspin - Update automatically ensures that the updates sub-key exists under the tailspin key and has the automatic value set to the string enable.
# yaml-language-server: $schema=https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json

Enforcing state

First apply

The first time the configuration applies to the machine, it creates the registry keys and value.

$Configuration = Get-Content -Path ./registry.config.dsc.yaml
$Configuration | dsc config set
results:
- name: Tailspin Key
  type: Microsoft.Windows/Registry
  result:
    beforeState:
      $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json
      keyPath: ''
    afterState:
      $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json
      keyPath: HKCU\tailspin
    changedProperties:
    - keyPath
- name: Tailspin - Update automatically
  type: Microsoft.Windows/Registry
  result:
    beforeState:
      $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json
      keyPath: ''
    afterState:
      $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json
      keyPath: HKCU\tailspin\updates
      valueName: automatic
      valueData:
        String: enable
    changedProperties:
    - keyPath
    - valueName
    - valueData
messages: []
hadErrors: false

The operation changed the keyPath property for the Tailspin Key instance. It changed the keyPath, valueName, and valueData properties for the Tailspin - Update automatically instance.

Second apply

The first time the configuration applies to the machine, it makes no changes. The instances are already in the desired state.

$Configuration | dsc config set
results:
- name: Tailspin Key
  type: Microsoft.Windows/Registry
  result:
    beforeState:
      $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json
      keyPath: HKCU\tailspin
    afterState:
      $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json
      keyPath: HKCU\tailspin
    changedProperties: []
- name: Tailspin - Update automatically
  type: Microsoft.Windows/Registry
  result:
    beforeState:
      $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json
      keyPath: HKCU\tailspin\updates
      valueName: automatic
      valueData:
        String: enable
    afterState:
      $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json
      keyPath: HKCU\tailspin\updates
      valueName: automatic
      valueData:
        String: enable
    changedProperties: []
messages: []
hadErrors: false