#PSTip Create your own DSC resource snippets in PowerShell ISE

Note: This tip requires PowerShell 4.0 or above.

PowerShell ISE 4.0 comes with just two DSC-related snippets (DSC Configuration (simple) and DSC Resource Provider (simple)). (Be aware that DSC Configuration (simple) snippet has a bug and uses the Requires property instead of new DependsOn property.).

What about DSC resources? How can we get info about their properties and valid values? You can take the cursor to the place where you have the name of the resource, press CTRL+Space, and a resource syntax will pop up:

DSC Service resource syntax

But, wouldn’t it be nice if we could get DSC snippets with a syntax for all our DSC resources? Luckily, the Get-DscResource cmdlet has the -Syntax parameter that we can use:

PS C:\> Get-DscResource -Name service -Syntax
Service [string] #ResourceName
    Name = [string]
    [ BuiltInAccount = [string] { LocalService | LocalSystem | NetworkService }  ]
    [ Credential = [PSCredential] ]
    [ DependsOn = [string[]] ]
    [ StartupType = [string] { Automatic | Disabled | Manual }  ]
    [ State = [string] { Running | Stopped }  ]

The easiest way to create DSC snippets is to enumerate all DSC resources, get the syntax using the Get-DscResource cmdlet, and pass it to New-IseSnippet cmdlet. Let’s wrap all that in a simple function called New-DscSnippet:

function New-DscSnippet {

   $resources = Get-DscResource | select -ExpandProperty name

   foreach ($resource in $resources) {

       $text = Get-DscResource -Name $resource -Syntax
       New-ISESnippet -Text $text -Title "DSC $resource Resource" -Description "Full DSC $resource resource syntax" -Force

Get-DscResource is a very slow cmdlet and this function will need quite some time to create all DSC snippets, but at the end you will get very usable code snippets to use from now on.


About the author: Aleksandar Nikolic

Aleksandar is a PowerShell MVP and a co-founder of PowerShell Magazine. He is a frequent speaker at the conferences and participates regularly in IT Pro user groups worldwide. Recently, Aleksandar co-authored the highly respected Administrator’s Guide to Windows PowerShell Remoting. Follow him on Twitter at @alexandair

Related Posts