PowerShell DSC Resource Cheat Sheet

Archive Resource Example

The Archive resource gives you a mechanism to unpack archive (.zip) files at a specific path.

Archive ArchiveExample {
 Ensure = "Present" # You can also set Ensure to "Absent"
 Path = "C:\Users\Public\Documents\Test.zip"
 Destination = "C:\Users\Public\Documents\ExtractionPath"
}

Script Resource Example

The Script resource gives you a mechanism to run Windows PowerShell script blocks on target nodes. The TestScript block runs first. If it returns False, the SetScript block will run. The GetScript block will run when you invoke the Get-DscConfiguration cmdlet (more on that cmdlet on the flipside of this sheet). GetScript must return a hash table.

Script ScriptExample
{
 SetScript = {
 $sw = New-Object System.IO.StreamWriter("C:\TempFolder\TestFile.txt")
 $sw.WriteLine("Some sample string")
 $sw.Close()
 }
 TestScript = { Test-Path "C:\TempFolder\TestFile.txt" }
 GetScript = { <# This must return a hash table #> }
}

Registry Resource Example

The Registry resource gives you a mechanism to manage registry keys and values.

Registry RegistryExample
{
 Ensure = "Present" # You can also set Ensure to "Absent"
 Key = "HKEY_LOCAL_MACHINE\SOFTWARE\ExampleKey"
 ValueName ="TestValue"
 ValueData ="TestData"
}

Package Resource Example

The Package resource gives you a mechanism to install and manage packages, such as MSI and setup.exe packages, on a target node.

Package PackageExample
{
 Ensure = "Present" # You can also set Ensure to "Absent"
 Path = "$Env:SystemDrive\TestFolder\TestProject.msi"
 Name = "TestPackage"
 ProoductId = "663A8209-89E0-4C48-898B-53D73CA2C14B"
}

Environment Resource Example

The Environment resource gives you a mechanism to manage system environment variables.

Environment EnvironmentExample
{
 Ensure = "Present" # You can also set Ensure to "Absent"
 Name = "TestEnvironmentVariable"
 Value = "TestValie"
}

Group Resource Example

The Group resource gives you a mechanism to manage local groups on the target node.

Group GroupExample
{
 # This will remove TestGroup, if present
 # To create a new group, set Ensure to "Present"
 Ensure = "Absent"
 GroupName = "TestGroup"
}

User Resource Example

The User resource gives you a mechanism to manage local user accounts on the target node.

User UserExample
{
 Ensure = "Present" # To delete a user account, set Ensure to "Absent"
 UserName = "SomeName"
 Password = $passwordCred # This needs to be a credential object
 DependsOn = “[Group]GroupExample" # Configures GroupExample first
}

Service Resource Example

The Service resource gives you a mechanism to manage services on the target node.

Service ServiceExample
{
 Name = "TermService"
 StartupType = "Manual"
}