Using Chef to create tasty recipes to manage Windows servers

The popular management configuration tool Chef uses the concept of Infrastructure as a Code and it supports on-premises, cloud, and hybrid environments. Chef works with several operating systems, including Microsoft, Linux (Red Hat, Ubuntu, Oracle Linux, CentOS, Amazon Linux, SUSE), AIX, FreeBSD, and MacOS. Using Chef, an organization can manage tons of servers and automate the deployment process from a single location, using a consistent process without human intervention. Chef makes it easier because it does not require a lot of scripting — just a few lines of code and the “chef” will take care of the changes at the operating system level.

In the Windows world, an administrator is able to manage several areas of the operating system, such as PowerShell, MSI packages, manage folders, files, registry, services, Windows tasks, environment variables, and more. All those areas of the operating system are known as Resources in Chef’s world. They are well documented and the administrator can check all options available for each resource built in on the solution on Chef’s website.

In this article, we will work with Chef Development Kit (Chef-DK), which is installed on a workstation to test the recipes. In a second article, we will apply our knowledge acquired in this article and we will be creating our first cookbook and applying the recipes to a couple of servers.

Another key point when creating recipes is to understand the Ruby language, which is not hard to learn and it is required when working with recipes.

Installing Chef Development Kit (ChefDK)

The first step is to download the Chef Deployment Kit and there are a couple of ways of doing that. We can use the following PowerShell script, which will invocate the installation file from a website, the code is shown below.

. { iwr -useb } | iex; install -project chefdk -channel stable -version 2.3.4

The second method is downloading and installing it from here. The installation is straightforward — just leave the default settings to complete the process. After installing the product an icon called Chef Development Kit will be displayed on the Desktop.

Note: When opening for the first time, you may have to change the Execution Policy of your system, running the following cmdlet:
Set-ExecutionPolicy Unrestricted

After opening the Chef Development Kit, which is a PowerShell prompt, we can use the command chef –version to identify the current version running on our workstation.

We are going to write some code and for that we need a powerful editor. My preference is to use Visual Studio Code, which can be obtained here. It is free and works on Windows, Linux, and Mac.

The installation process is straightforward. Just leave the default settings to get the product installed, although I recommend selecting the option to create an icon on the desktop and register the product on the Explorer shell as well.

In the same workstation, we need to prepare the directory structure to support our upcoming cookbooks and recipe testing that we will be performing. We will be creating two (2) folders, as follows:

mkdir C:\Chef
mkdir C:\Chef\CookBooks

Creating a Folder

One of the first steps is creating a folder. For that, we will create a file on Visual Studio Code called directory.rb, and we will save it under C:\Chef folder. In order to create a directory using Chef, we can add only these lines listed below. The beauty of the automation is that we did not have to specify the actual command on Windows (mkdir or New-Item in PowerShell) to get a folder created.

Keep in mind that we are keeping things simple here. Using Chef we manage permissions, remove folders, and so forth. Another point is that the default value for action is always created, which means that line could be easily omitted from the script.

directory ‘C:\AppX’ do

We have a simple Ruby file, and running the cmdlet below we will run the file on local mode. The output will describe the recipe (the directory.rb) and its activities, as depicted in the image below.

Chef-client--local-mode .\directory.rb

Creating and managing a file

A file is another resource available with Chef, and we can manage content and the files itself using it.

In this article, we will make sure that we add a file called app.conf inside of the folder that created previously and we will add a line (

file ‘C:\AppX\app.conf’ do
content ‘’

Understanding Infrastructure as Code

Again, we are really keeping it simple here, but the following example will give an idea how Chef helps an organization keep systems up and running. Let’s assume that an administrator went to one of the servers where we used Chef to configure the app.conf file and changes to a wrong value, something like

If we run the same cmdlet again, the Chef client will identify that it is not the same and it will overwrite the value, as depicted in the image below. The best practice requires the Chef Client to run every X amount of time on the servers to make sure that they are always consistent.

If we look closely, we can see the previous information, which was removed and the new information added based on our recipe file.

The same rule applies when we change a configuration on a server farm, and we want to make sure that those changes were applied.

What’s next?

In this article, we had a glimpse how to automate two simple tasks on Windows Server using Chef and its concept of Infrastructure as a Code.

By performing just these two simple tasks, we can see how this will be useful for a range of solutions. For example: if we have several web application servers with static content to be deployed, we could use Chef to create the folders, add the files, and the server will be up and running in no time.

You may be wondering, how about the IIS? That is not a problem. We can automate IIS deployment and control the service status, and we will cover that in a future article here at TechGenix.

Photo credit: Pixabay

Anderson Patricio

Anderson Patricio is a Canadian MVP in Cloud and Datacenter Management, and Office Server and Services, besides of the Microsoft Award he also holds a Solutions Master (MCSM) in Exchange, CISSP and several other certifications. Anderson contributes to the Microsoft Community with articles, tutorials, blog posts, twitter, forums and book reviews. He is a regular contributor here at,, and Anderson (Portuguese).

Published by
Anderson Patricio
Tags Chef

Recent Posts

Docker Enterprise 3.0 introduces new tools for container-based applications

Docker Enterprise 3.0, currently out in public beta, lets developers build container-based multiservice applications right on their desktop.

1 hour ago

U.S.-China trade war fallout: Google cuts off Huawei’s access to Android

Google has cut off Huawei from access to its Android operating system. What does this mean for users of the…

3 hours ago

Why wireless sensor networks for datacenters are becoming a must

Energy consumption is one of the biggest problems facing datacenters, and possibly even the world, today. Before we talk about…

5 hours ago

How PowerShell transcription logs can boost your system security

Worried someone may be using PowerShell for nefarious purposes? Here is how to leverage PowerShell transcription logs to see what’s…

7 hours ago

WhatsApp spyware attack: Everything you need to know

With its end-to-end encryption, Facebook-owned WhatsApp was considered to be a highly secure app — until this serious vulnerability was…

3 days ago

10 endpoint security issues every SMB faces — and what to do about them

How can small and medium-sized businesses protect themselves against the onslaught of cyberthreats? Begin with endpoint security.

3 days ago