Creating Multiple Directories

I’ve used the “File” resource to create an occasional folder for items such as temp files and logs. I’m looking to create and ensure some nested directories for an application on a server.

In all, I have around 20 folders to create in this fashion.
Should I create a definition for each folder using the file resource or is there a cleaner way?


I’ve hacked up a bit of code I’ve been using inside a composite resource.

Basically, using the ConfigurationData parameter of the DSC configuration object, we can pass an object containing all of our config to the internal resources.

So, here is our configuration.

Configuration MyConfiguration {

  Node "Localhost" {      

    ForEach ($Folder in $Node.FolderStructure) {

      # Each of our 'file' resources will be named after the path, but...
      #   we have to replace : with __ as colons aren't allowed in resource names
      File $Folder.Path.Replace(':','__') {
        DestinationPath = $Folder.Path
        Ensure = $Folder.Ensure

    } # ForEach
  } # Node "Localhost"

} # configuration MyConfiguration

Now, our configuration data variable.

$ConfigurationData = 
    AllNodes = @(
            NodeName = "localhost"

            FolderStructure = @(

                    Path =   "D:\Management\Packages"
                    Ensure = "Present"

                    Path =   "D:\Management\Wallpaper"
                    Ensure = "Present"

            ) #FolderStruture = @(...

        } # localhost
    ) # AllNodes = @(...

As you can see, the configuration contains a File resource that uses the information in the ConfigurationData variable. It loops through ‘FolderStructure’ in $ConfigurationData, so if we need to add any more, we just add them there. You don’t have to do it exactly like this, but this allows you to add more folders without having to repeat a lot of code (Plus, you can keep your ConfigurationData variable separate to the rest of the actual DSC configuration gubbins).

Also, another thing you can do is - And I don’t know if this is really the proper way to do it - use the Recurse option in the File resource to automatically create all folders in the path you supply. Its real use is to copy full file paths, but if you specify an empty folder as the ‘SourcePath’, it will create each folder in turn. For example…

File MyFile {
    DestinationPath = 'D:\A\Really\Long\File\Path'
    SourcePath = 'C:\Windows\Temp\ABlankFolder'
    Recurse = $true
    Ensure = 'Present'

It’s probably best to add some logic to create ‘C:\Windows\Temp\ABlankFolder’ before you use it though.

I like the first approach. I considered making a composite resource to accomplish the example you demonstrated. As for the second approach, I considered this one was well. Proper or not, I personally prefer to keep the structure in a configuration file and not dependant on say a blank folder structure on my repo server.

Thanks for the response James, I’m fairly new to DSC and you provided some nice insight.