Hyper-V is the leader in virtualization infrastructure in Windows. Hyper-V is a native virtualization platform within Windows, so there is no need to buy any new software. Hyper-V can deploy both Windows and Linux operating systems. The best part is it's completely free on Windows Server!

Hyper-V, however, does not come pre-configured out of the box with Windows Server. To configure Hyper-V, an automated approach can be used with PowerShell Desired State Configuration, also known as DSC. In any environment, there needs to be a fast, easy, and efficient way to spin up Windows Server features.

PowerShell DSC is a configuration management tool for managing Windows Servers. DSC allows both maintenance management and installation of roles and features. DSC gives the declarative model needed to build an automated Hyper-V deployment.

Before the days of PowerShell DSC, engineers were expected to manually deploy software and components. This could take hours to days. Luckily with automation, organizations don't need to worry about those manual tasks anymore. If you never want to manually do the desired state of a server again, keep reading!

Prerequisites

Before getting started, there are some prerequisites that are needed. The first prerequisite is having a Windows Server. I'm using Windows Server 2019 for my installation of Hyper-V. Any Windows Server version will work as long as it has PowerShell V4 or above. PowerShell V4 is on Windows Server 2012 and above.

The second prerequisite is a place to write the PowerShell DSC configuration. You can easily do that in PowerShell ISE or another text editor on Windows Server.

What Will You Be Doing?

The scope is to create a server that is running Hyper-V. To do this you will:

  • Log into a Windows Server
  • Open up a text editor (like PowerShell ISE)
  • Create a PowerShell DSC configuration in the text editor
  • Run the DSC configuration
  • Confirm the configuration worked.

Creating the PowerShell DSC Configuration Script

In the previous sections, you took a look at the prerequisites and what you'll be configuring. In this section its time to get your hands dirty and start writing some code.

Access the Server

RDP (Remote Desktop) into the Windows Server and from the search bar, type PowerShell ISE.

Choose PowerShell ISE and it will be opened.

Writing the DSC Configuration

In this section, you will write the code for the DSC configuration needed to install Hyper-V.

The DSC configuration is written below and you can copy/paste into your text editor. You can also find the DSC configuration on my GitHub repo here.

The DSC configuration is calling the WindowsFeature flag.

Configuration HyperVInstall {

    param
    (
        [string[]]$ComputerName='localhost'
    )

    Node $ComputerName {
        WindowsFeature Hyper-V {
            Ensure = 'Present'
            Name= "Hyper-V"
        }
    }
}

HyperVInstall

Running the PowerShell DSC Configuration

Now that the code is written for the Hyper-V DSC configuration, it's time to run it.

Click the green Play button as shown in the screenshot below to commit the configuration into memory and to create the DSC configuration file.

In the output, there will be a MOF file, which is the configuration where Hyper-V settings have been stored. The path where the MOF is created for you will be different depending on where you run the PowerShell DSC configuration.

Installing the DSC Configuration on the Server

In the previous section, you ran the DSC configuration to generate the MOF file. Now it's time to run that MOF file to deploy Hyper-V to the Windows Server.

From a PowerShell Administration prompt run:

Start-DscConfiguration -Path .\\HyperVInstall\\ -ComputerName localhost -Verbose

This could take a little while, so adding the -verbose flag will show step-by-step on what's happening in the configuration deployment.

Conclusion

After this process is complete you will have a brand new Hyper-V server! PowerShell DSC is a fast, reliable, and secure way to spin up any role or feature on Windows Server. With the capabilities built right in, it's hard to say no to the automation features that DSC gives us.