Occasionally, I mention that I use saved scripts to auto-configure the Hyper-V hosts in my lab. Small, homegrown tools like this are perfect for smaller installations that can’t justify enterprise tools like VMM and SCOM, which can handle a lot of situations like this automatically. However, I never shared any of those tools because they just weren’t very high quality. They did what I needed, but I understood them and could fix any problems easily enough that I never bothered to make any of the scripts particularly resilient.

Until now.

What follows is a thoroughly retooled and streamlined version of all the separate little things I’ve built for myself over the past few years to facilitate host rebuilds, and configure a Hyper-V Host. It’s not perfectly bullet-proof and I did not build in any recovery problems from errors. However, if set correctly, it should leave you with a perfectly functional host.

Script Discussion

Normally, I post the script first so that you can copy/paste it and be on your way. This one requires you to understand some things up front. First: it is not in a usable state. You will need to modify it before you can use it. When I sat down to work something up for you to use, I had a lot of choices on the way that it could have gone. What I decided was that it needed to be a one-stop experience — no digging around in other files, no organizing things, no re-tooling Windows images, no running one tool in order to run another tool, etc. That’s because I’ve been in a small business environment and I know that “automation” processes that take as much effort as just doing the work by hand are not going to be followed. So, if you’re looking at anything in here and wondering why I did it that way, the reason is almost certainly related to this philosophy.


To use this script, you need:

  • A way to reinstall Windows Server 2012 R2 or Hyper-V Server 2012 R2 on the target host
  • A way to edit a PowerShell file. PowerShell ISE, Visual Studio 2015 Community Edition with PowerShell Tools, and Notepad are all freely available solutions that will suffice. The first two might serve you better because I seeded the beginning of the file with a table of contents with line numbers, and Notepad does not display line numbers. As you use the file, the line numbers will likely be changed anyway, so that might not help much. I used another technique that will work just fine with CRTL+F in all three tools. You’ll see that in the instructions.

Nice to have, but not required, is the raw files for any drivers that you want to install.

I choose to install Windows and Hyper-V Server my physical hosts by using an 8GB USB stick. This is because I can drop this file and anything else that I need right on it without modifying an ISO file. I can also tinker with it, or delete anything on it, without any effort. I found these directions once upon a time on a Stack Overflow site but can no longer find my way back to the exact answer for proper crediting, so apologies to whomever I stole this from. To prepare a USB disk to be bootable and hold Windows installation files, use DISKPART.EXE with the following commands:

Once the USB stick is prepared, copy the entire contents of a Windows/Hyper-V Server ISO to it.

Next, follow the directions in the script. If you do not follow the directions, I left a reminder for you. Place the edited file on the USB stick. If you have drivers, place them as well. The script as-written looks for its drivers in sub-folders of a “Drivers” folder but you can do whatever you want.

The script as you see it is for one of my systems. Your task will be to change it for one of your systems.

It performs these operations in order:

  1. Very basic host configuration, like a name change.
  2. Installs drivers. Feed it .INF files.
  3. Enables roles and features. Hyper-V and MPIO are handled automatically.
  4. Reboots. You must have supplied a valid local administrator account in the beginning of the file or the whole thing will stop here. Remember that enabling the Hyper-V role requires two restarts.
  5. Configures physical adapters. My hosts do not support Consistent Device Naming (CDN) so I configure them by MAC. Tinker with the Get-NetAdapter lines to suit your system.
  6. Configures network team(s). Comment this section out as necessary.
  7. Configures virtual switches.
  8. Configures virtual network adapters for the management operating system.
  9. Joins the domain, if one is specified along with valid credentials for a domain account that has permissions to add computers to the domain.
  10. Reboots if domain join was successful.
  11. iSCSI is configured. I placed the iSCSI section here because you might have configured your target to expect particular initiators, and initiators for a host change when it joins a domain.
  12. Some Hyper-V defaults are set.
  13. Any customizations that you make are performed.

Everything that this script does is recorded to C:\Windows\Logs\RebuildHost.log. Absolutely read this file after each use.

I tried to make it very clear where you need to make changes and how they need to be made. Just follow my lead. The toughest spot will probably be the iSCSI section. There are so many possible ways to configure iSCSI that it’s tough to make an all-purpose script.

WARNING/DISCLAIMER: The following script is intended to be run on a freshly installed Windows Server or Hyper-V Server host. It will cause catastrophe-level changes if run on a functioning host, including but not limited to: all teams being deleted, all network information being lost, all virtual switches being deleted, and release of those sensitive pictures you have on your phone (just kidding on that last part). Neither Altaro Software nor I are responsible for any bad things that happen because of this script, especially if they happen because you didn’t read the instructions.

This is a copy of the RebuildHost.log file from my testing:


I’d like to know what you think of this. If you find it useful, please let me know. If there’s something that would be good to add for the community’s use, suggest it in the comments.