Deploy a custom Windows VHD in Azure with PowerShell and validate with Pester


Microsoft do not offers this from scratch on Azure VM. So we first need to build our own VHD file. Nothing difficult here, but let’s use the PowerShell way !

Build the image

First of all we need a Virtual Machine started in Hyper-V. I won’t explain how doing this on this post but you should do (at least)  the following things on it:

  • Enable RDP and open ports in Firewall
  • Enable WinRM and open ports in Firewall

With that you’ll be able to administrate your deployed server. Once modifications are done, you can no sysprep the system, using sysprep.exe.


Click on the sysprep Window and wait for the VM to sop by itself at the end of the process.

You now have a vhd or a vhdx file. If you have a vhdx file (like me) the first thing to do, is to convert it to vhd.

After a little time waiting for the command’s end, you now have a beautiful .vhd file.


Upload the VHD

In order to upload a VHD file to Azure, we need :

  1. Azure PowerShell SDK
  2. A publishsetting file
  3. An affinity group
  4. A Storage account
  5. A container

We already have our publishsetting file, we saw how to get it on this post 🙂

For the he affinity group creation and the storage account, you can check this post!

So let’s start the creation of the storage.

In this post we saw how to create our container.

Now, let’s upload the .vhd into this container.

The cmdlet will first calculate a MD5 Hash to guarantee the vhd file integrity and upload it to the container. It can takes few time 😉

Once the vhd is uploaded, we now have to add the vhd to Windows Images listing. Once again PowerShell to the rescue (you can do it by GUI, but you’re not on the right blog for that :p)

Pester Pester
Bonus: Here is a little Pester script to validate that everything is OK for your vhd deployment 🙂

If you still don’t know Why and How use Pester Framework, please follow this link!

Now you can deploy customized VMs with PowerShell or Chef.