Is it possible to create a generalized configuration file for installing Windows features using PowerShell?

Yes, for both Linux and Windows you can build desired state config files that can:

  • Enable or disable server roles and features
  • Manage registry settings
  • Manage files and directories
  • Start, stop, and manage processes and services
  • Manage groups and user accounts
  • Deploy new software
  • Manage environment variables
  • Run Windows PowerShell scripts
  • Fix a configuration that has drifted away from the desired state
  • Discover the actual configuration state on a given node

Here is a sample config file that will enable IIS, ensure that the website files are in the right folder, and if any of these things are not installed or missing, to install or copy them as appropriate (note that $websitefilepath is presumed to be predefined as the source for the website files):

    Configuration MyWebConfig
    {
       # A Configuration block can have zero or more Node blocks
       Node "Myservername"
       {
          # Next, specify one or more resource blocks

          # WindowsFeature is one of the built-in resources you can use in a Node block
          # This example ensures the Web Server (IIS) role is installed
          WindowsFeature MyRoleExample
          {
              Ensure = "Present" # To uninstall the role, set Ensure to "Absent"
              Name = "Web-Server"
          }

          # File is a built-in resource you can use to manage files and directories
          # This example ensures files from the source directory are present in the destination directory
          File MyFileExample
          {
             Ensure = "Present"  # You can also set Ensure to "Absent"
             Type = "Directory“ # Default is “File”
             Recurse = $true
             # This is a path that has web files
             SourcePath = $WebsiteFilePath
             # The path where we want to ensure the web files are present
             DestinationPath = "C:\inetpub\wwwroot"
   # This ensures that MyRoleExample completes successfully before this block runs
            DependsOn = "[WindowsFeature]MyRoleExample"
          }
       }
    }

For more details see Windows PowerShell Desired State Configuration Overview and Get Started with Windows PowerShell Desired State Configuration.

So why would you use this instead of simply the install-windowsfeature cmdlet? The real power of using DSC instead of a script is that I can define a location where I can store configurations to be pushed to or pulled from (with respect to the target machine) see Push and Pull Configuration Modes. The configuration doesn't care if the machine is physical or virtual, but I believe it takes at least 2012 to get the server to boot to pull DSC.


You can do it all in PowerShell

Get-WindowsFeature | ? { $_.Installed } | Export-Clixml .\installed.xml

Copy the xml where is needs to go to, somewhere the new server can access it.

Import-Clixml <path to xml>\installed.xml | Install-WindowsFeature