Creating a Windows Server Hard Drive “Template” for Hyper-V

This may not be the “official” way to create a template for Hyper-V according to Microsoft, but if you are running in an environment without SCCM or WDS (or on your personal machine), I’ve found that this process works very well.

This is fairly simple list of tasks, but it’s very long.  Thankfully, you’ll only need to do this once and then you can re-use the image as many times as you like.  I update mine once a quarter (to get the newest updates).

Tweak where it’s appropriate for your build.


Table of Contents

  • Building the first VM
  • Install Windows Server 2012 R2
  • Install Legacy .NET Framework (optional)
  • Install Utility Software
    • Install 7-Zip
    • Install Notepad++
    • Create Utility Folder
    • Copy Utilities (SysInternals & UnxUtils)
    • Update Path Environment Variable
  • Statically assign Page File size
  • Run Windows Update
  • Reset your Base Image
  • Defragment Hard Drive (Recommended)
  • Zero Disk (Recommended)
  • Sysprep the System
  • Optimize Virtual Hard Disk (Recommended)
  • Delete Virtual Machine
  • Using your Template Drive

Building the first VM

I won’t go into too much detail about building the initial VM because it’s pretty much self explanatory.  I will just way that I make sure that I use a Generation 2 Virtual Machine, have it boot from the Windows 2012 R2 ISO, give it 2 processors and 4GB of RAM.

Install Windows Server 2012 R2

If you can’t install Windows, then you are probably reading the wrong post.  This step is included here just for completeness.

Post-installation, I disable the Server Manager via the “Manage,” “Server Manager Properties.”

Before I close Server Manager, I also go into the Local Server and configure my time zone, Internet Explorer Security Settings (launch IE once to make sure that the settings took), and Remote Desktop.

Install Legacy .NET Framework (optional)

Since most of the software that I plan on loading in the future (SQL Server is one such example) requires the .NET 3.0 Legacy, I install it before going any further.  Assuming that your installer ISO is still mounted as the D: Drive, run the following PowerShell.

Now you can eject the ISO – it’s no longer needed.

Install Utility Software

There are a few programs that I pretty much require on all machines that I use.  I’ve never found that they generate a conflict for me, so until they do, I will install them.

Install 7-Zip

love 7-Zip.  It’s been my go-to compression tool for a few years now.  I’m lazy and install it with PowerShell.

Install Notepad++

My love is only slightly less passionate for Notepad++.  It’s a simple tool, but the huge list of various plugins make it one of my favorite editors ever.  I also install it with PowerShell.

I’m a fan of the “Compare”, “XML Tools” and “JSON Viewer” plugins, so I add them via the Plugins, Plugin Manager.

Create Utility Folder

As you can see, I’m a fan of utilities on my servers.  I’ve used the SysInternals and UnxUtils tools for a few years now and put them on pretty much every machine that I touch.  To start, I just create a new folder in the root of the C: Drive called “UTILS” (complex, right?)

Copy Utilities (SysInternals & UnxUtils)

Download the SysInternals Suite, extract & copy to the UTILS folder.

Download the UnxUtils, extract & copy the contents of the “usr\local\wbin” to the UTILS folder.

Update Path Environment Variable

We need to add the UTILS folder path to the Machine’s environment variable.  You can do this with PowerShell, but I got lazy on this one.  I just opened the System Control Panel object, went into Advanced Settings, clicked on Environment Variables, and appended “;C:\UTILS” to the end of the PATH variable under System variables (lower box).  Click on the OK’s to close the windows.

Test it by launching the command prompt and typing “grep –help” – if you get a response, then the path has been updated.

Statically assign Page File size

This is the “gotcha” part.  Since I’ll be running mostly 4GB machines off this image, I can statically assign the Page file size.  In my case, I set it to 4 GB + 257MB (for a total of 4353 MB) per recommendations on this Microsoft Support Page.  Skip this if you won’t remember to check on these changes when re-deploying the template.

Run Windows Update

Prepare for Nap Time.  Now that you have the “base” of your hard drive, it’s time to update it.  Since it’s been several years since Windows 2012 R2 was released, there are a bunch of updates (over 200 at the time of this writing) and they take up a few gigs of downloads.

Get started and run Windows Update, accept all updates, reboot, repeat.

Do this until there are no updates left to install, then reboot once more and check again (just to be safe).

Reset your Base Image

Since this is your new “starting point” clean it up by resetting the base image.  This process will essentially “lock in” any updates that you’ve done up to this point and prevent any from being uninstalled in the future.  I got this tidbit from Microsoft.

From the command prompt, run…

Bonus:  It also shrinks your installation size, so that your new template hard drive is smaller.

Defragment Hard Drive (Recommended)

Do you need to do this?  Probably not.  It’s probably just me harkening back to the days of Windows 98.  Nevertheless, I do it using the Auslogics Portable Defrag.  I use the Defrag and Optimize option.

Zero Disk (Recommended)

This one you also don’t need to do, but it does help reduce the size of the hard drive.

Launch a command prompt and run…

What’s it doing?  It’s going through your hard drive and finding any empty sectors on the drive and setting them to zero.  When it’s complete, you may not see a change in the hard drive size – that’s ok.  We’ll take care of that later.

Sysprep the System

We’re almost at the finish line.  Let’s now prepare this image for re-use.

To do this run sysprep – you can find it at C:\Windows\System32\sysprep\sysprep.exe.

sysprepSelect OOBE, Generalize, and Shutdown.

If you turn your VM back on after this, you’ll need to re-run this step.

Optimize Virtual Hard Disk (Recommended)

Now that we have our “image,” we should make it as small as possible so that we can conserve disk space.

Open PowerShell and run…

Sit back and wait.  You should see a reduction in the size – mine went from 27 GB down to 12 GB.  Now that’s some cleanup!

Before Optimize
Before Optimize-VHD
After Optimize
After Optimize-VHD

Delete Virtual Machine

I know that you’ve gotten attached to this virtual machine, but it’s time to say goodbye.  Delete the virtual machine from the Hyper-V Manager (this won’t delete the hard drive).  I’d recommend making a second copy somewhere and marking your primary copy as Read-Only.

Now you have your template image for Windows Server 2012 R2 with updates through today!

Using your Template Drive

To use your template drive, create a new (Gen 2) virtual machine and do NOT add a hard drive.

Then go into settings and add a hard drive.  Click on the New button and go through the wizard, then select Copy the Contents…

ConfigureDisk

That’s it.  Now you have a new system with the updates ready to go!

Until next time ramblers…

 

Leave a Comment