Bird
Raised Fist0
PowerShellscripting~10 mins

Desired State Configuration (DSC) basics in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Desired State Configuration (DSC) basics
Write DSC Configuration Script
Compile Configuration -> MOF File
Apply MOF to Target Node
DSC Agent Checks Current State
Compare Current State with Desired State
Do Nothing
Report Success
DSC runs by writing a configuration script, compiling it to a MOF file, then applying it to a target node where the DSC agent ensures the system matches the desired state.
Execution Sample
PowerShell
Configuration SampleConfig {
  Node localhost {
    File ExampleFile {
      DestinationPath = 'C:\\example.txt'
      Contents = 'Hello DSC!'
      Ensure = 'Present'
    }
  }
}

SampleConfig
Start-DscConfiguration -Path .\SampleConfig -Wait -Verbose
This script defines a DSC configuration to create a file with specific content and applies it to the local machine.
Execution Table
StepActionEvaluationResult
1Define Configuration SampleConfigScript parsedConfiguration block created
2Compile SampleConfigMOF file generatedSampleConfig.mof created
3Start-DscConfiguration with MOF pathDSC agent reads MOFDSC agent starts applying configuration
4Check if 'C:\example.txt' exists with content 'Hello DSC!'File missing or content differentFile created with correct content
5Verify file stateFile exists with correct contentDSC reports success
6EndNo further actionsConfiguration applied successfully
💡 DSC stops after confirming the system matches the desired state defined in the MOF file.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Configuration ScriptNot definedDefinedDefinedDefinedDefined
MOF FileNot createdCreatedCreatedCreatedCreated
DSC Agent StateIdleIdleApplyingApplyingIdle
File 'C:\example.txt'Does not existDoes not existDoes not existCreated with contentExists with content
Key Moments - 3 Insights
Why does DSC create a MOF file before applying configuration?
The MOF file is a compiled version of the configuration script that the DSC agent reads to know the desired state. This is shown in execution_table step 2 where the MOF file is generated before applying.
What happens if the file already exists with the correct content?
DSC detects the current state matches the desired state and does nothing, as seen in execution_table step 4 where the check would find the file present and skip creation.
Why does DSC report success after applying changes?
Because after making changes, DSC verifies the system matches the desired state, confirming success as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of step 4?
AMOF file generated
BFile created with correct content
CDSC agent starts applying configuration
DNo further actions
💡 Hint
Check the 'Result' column in step 4 of the execution_table.
At which step does the DSC agent begin applying the configuration?
AStep 3
BStep 5
CStep 2
DStep 1
💡 Hint
Look at the 'Action' and 'Result' columns to find when the DSC agent starts.
If the file already exists with the correct content, what would change in the execution_table?
AStep 4 would show 'File created with correct content'
BStep 4 would show 'File missing or content different'
CStep 4 would show 'File exists with correct content' and no creation action
DStep 5 would be skipped
💡 Hint
Refer to the key_moments explanation about existing file state and step 4.
Concept Snapshot
Desired State Configuration (DSC) basics:
- Write a configuration script defining desired state.
- Compile script to MOF file.
- Apply MOF to target node.
- DSC agent compares current vs desired state.
- Makes changes if needed, then reports success.
- Ensures system stays in desired state automatically.
Full Transcript
Desired State Configuration (DSC) is a way to make sure a computer system stays in a specific setup you want. First, you write a configuration script that says what you want, like creating a file with certain content. Then, you compile this script into a MOF file, which is like a set of instructions. You apply this MOF file to the computer, and the DSC agent on that computer reads it. The agent checks if the current system matches what you want. If not, it fixes things, like creating the file. Once everything matches, DSC reports success and keeps the system in that state. This process helps keep computers consistent and saves time from fixing things manually.

Practice

(1/5)
1. What is the main purpose of Desired State Configuration (DSC) in PowerShell?
easy
A. To monitor network traffic continuously
B. To write scripts that run only once manually
C. To automatically keep your computer setup as you want it
D. To create user accounts interactively

Solution

  1. Step 1: Understand DSC's role

    DSC is designed to keep system configurations consistent automatically.
  2. Step 2: Compare options

    Only To automatically keep your computer setup as you want it describes automatic maintenance of computer setup, which is DSC's goal.
  3. Final Answer:

    To automatically keep your computer setup as you want it -> Option C
  4. Quick Check:

    DSC purpose = automatic setup maintenance [OK]
Hint: DSC means automatic setup enforcement [OK]
Common Mistakes:
  • Thinking DSC only runs scripts once
  • Confusing DSC with network monitoring
  • Assuming DSC manages user accounts interactively
2. Which of the following is the correct way to start a DSC configuration block in PowerShell?
easy
A. Start-DSCConfig MyConfig { }
B. Configuration MyConfig { }
C. Config MyConfig { }
D. New-DSCConfig -Name MyConfig { }

Solution

  1. Step 1: Recall DSC syntax

    The DSC configuration block always starts with the keyword 'Configuration' followed by the name.
  2. Step 2: Check options

    Only Configuration MyConfig { } uses the correct keyword 'Configuration' to define the block.
  3. Final Answer:

    Configuration MyConfig { } -> Option B
  4. Quick Check:

    DSC block starts with 'Configuration' [OK]
Hint: DSC configs start with 'Configuration' keyword [OK]
Common Mistakes:
  • Using 'Config' instead of 'Configuration'
  • Trying to use cmdlets to start config blocks
  • Confusing DSC syntax with other PowerShell commands
3. Given this DSC configuration snippet, what will be the output file name generated after running MyConfig?
Configuration MyConfig {
  Node 'localhost' {
    File ExampleFile {
      DestinationPath = 'C:\temp\example.txt'
      Contents = 'Hello DSC'
    }
  }
}
MyConfig
medium
A. localhost.mof
B. MyConfig.ps1
C. example.txt
D. MyConfig.mof

Solution

  1. Step 1: Understand DSC output files

    Running a DSC configuration generates a MOF file named after the Node, here 'localhost'.
  2. Step 2: Identify the MOF file name

    The MOF file will be 'localhost.mof' because the Node is 'localhost'.
  3. Final Answer:

    localhost.mof -> Option A
  4. Quick Check:

    DSC MOF file = NodeName.mof [OK]
Hint: DSC MOF file named after Node, not config [OK]
Common Mistakes:
  • Assuming MOF file is named after config
  • Confusing MOF with script files
  • Expecting output as the destination file
4. You wrote this DSC configuration but get an error when running it:
Configuration SampleConfig {
  Node 'localhost' {
    File MyFile {
      DestinationPath = 'C:\temp\file.txt'
      Content = 'Test content'
    }
  }
}
SampleConfig
What is the error in this script?
medium
A. The Node name 'localhost' is invalid
B. The configuration block must end with 'End-Configuration' command
C. The DestinationPath must be a folder, not a file
D. The property 'Content' should be 'Contents' in the File resource

Solution

  1. Step 1: Check File resource properties

    The File resource requires the property 'Contents' (plural), not 'Content'.
  2. Step 2: Validate other parts

    Node 'localhost' is valid, DestinationPath can be a file path, and no 'End-Configuration' command exists.
  3. Final Answer:

    The property 'Content' should be 'Contents' in the File resource -> Option D
  4. Quick Check:

    File resource uses 'Contents' property [OK]
Hint: File resource property is 'Contents' plural [OK]
Common Mistakes:
  • Using 'Content' instead of 'Contents'
  • Thinking Node names must be IP addresses
  • Expecting a special end command for configuration
5. You want to ensure a folder C:\Logs always exists on a remote computer using DSC. Which configuration snippet correctly enforces this desired state?
hard
A.
Configuration EnsureFolder {
  Node 'RemotePC' {
    File LogsFolder {
      DestinationPath = 'C:\Logs'
      Type = 'Directory'
      Ensure = 'Present'
    }
  }
}
B.
Configuration EnsureFolder {
  Node 'RemotePC' {
    Directory LogsFolder {
      Path = 'C:\Logs'
      State = 'Exists'
    }
  }
}
C.
Configuration EnsureFolder {
  Node 'RemotePC' {
    File LogsFolder {
      DestinationPath = 'C:\Logs'
      Ensure = 'Present'
    }
  }
}
D.
Configuration EnsureFolder {
  Node 'RemotePC' {
    File LogsFolder {
      DestinationPath = 'C:\Logs'
      Type = 'File'
      Ensure = 'Present'
    }
  }
}

Solution

  1. Step 1: Identify correct resource and properties

    To ensure a folder exists, use the File resource with Type='Directory' and Ensure='Present'.
  2. Step 2: Check options for correctness

    Configuration EnsureFolder {
      Node 'RemotePC' {
        File LogsFolder {
          DestinationPath = 'C:\Logs'
          Type = 'Directory'
          Ensure = 'Present'
        }
      }
    }
    correctly uses File resource with Type='Directory' and Ensure='Present'.
    Configuration EnsureFolder {
      Node 'RemotePC' {
        File LogsFolder {
          DestinationPath = 'C:\Logs'
          Ensure = 'Present'
        }
      }
    }
    misses Type, D wrongly uses Type='File', B uses a non-existent Directory resource.
  3. Final Answer:

    Option A snippet correctly ensures folder presence -> Option A
  4. Quick Check:

    File resource + Type='Directory' + Ensure='Present' = folder exists [OK]
Hint: Use File resource with Type='Directory' to create folders [OK]
Common Mistakes:
  • Using Type='File' for folders
  • Omitting Type property for directories
  • Using non-existent 'Directory' resource