4

#PSTip Create an empty folder/file using Desired State Configuration File resource

Note: This tip requires PowerShell 4.0 and Windows Server 2012 R2 or Windows 8.1.

When using the File resource in DSC, a confusing aspect to beginners is how to create an empty folder. If you look at the definition of this resource and the attributes, it gives an impression that you can only copy files/folders from SourcePath to DestinationPath. Well, that is not entirely accurate. You can also create empty folders and files.

For creating an empty folder, use the following configuration document. All you need is the DestinationPath and Type set to Directory.

Configuration FileDemo {
    Node SRV1-WS2012R2 {
        File FileDemo {
            Type = 'Directory'
            DestinationPath = 'C:\TestUser3'
            Ensure = "Present"
        }
    }
}

FileDemo

This will create an empty folder!

Now, how do we create an empty file? Simple. We provide the path to the file as DestinationPath and supply an empty string as the value to Contents attribute.

Configuration FileDemo {
    Node SRV1-WS2012R2 {
        File FileDemo {
            DestinationPath = 'C:\TestUser3\Test.txt'
            Ensure = "Present"
            Contents = ''
        }
    }
}

FileDemo
Filed in: Columns, Tips and Tricks Tags: , ,

4 Responses to "#PSTip Create an empty folder/file using Desired State Configuration File resource"

  1. James Field says:

    Hi Ravikanth,

    I first heard about you when you wrote the (in my opinion) seminal ebook regarding powershell remoting. I subsequently purchased your new APress book on Powershell DSC and was similarly impressed.

    Having dipped in and out of your book enough to get me started down the road of using DSC, I had hit a stumbling block of how to create an empty folder resource. Before using the good old fashioned approach of trial and error I performed a quick Google search. Sure enough your site was the first valid response back.

    It now seems, there is no Powershell related question I might ask that you cannot answer.

    Thank you very much sir!

  2. Cadorex says:

    Hello,

    Thanks for all your help.

    I would like know with DSC how i can change value in “file.txt.” ?

    With Powershell i can use that to change value but how to do it with DSC ?

    $content = Get-Content “.\file.txt” | foreach { $_ -replace “hi”,”bonjour” }
    Set-Content -Path “.\file.txt” -Value $content

    Thank you 🙂

    • Kareem says:

      I have used Script module to get that done.

      For example:
      Script EditBuildAgentInfo
      {
      GetScript = {
      return $null;
      }

      SetScript ={
      Write-Verbose “Changing File content name in the properties file”;
      $name = $using:Node.buildagent_name;
      (Get-Content E:\BuildAgent\conf\buildAgent.properties) | ForEach-Object { $_ -replace “^name=.+” , “name=$name” } | Set-Content E:\BuildAgent\conf\buildAgent.properties
      }
      TestScript = {
      return $false;
      }

      }

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: