Thursday, May 26, 2011

Installing Windows is boring

So why not automate it?
I’m a big fan of virtual machines and especially booting from VHD natively in Windows 7 (as you might have guesed from my previous post).

On those virtual machines I run Windows 7 for the most part. Creating new VHD is pretty easy, but installing the OS and everything you need is a tedious task.

One way to solve this is to create a parent VHD with Windows 7 and then create child VHD based on this one. This is what I do for the most part, but sometimes I just need a fresh install of Windows 7.

I could off course do this by creating a blank VHD, boot into it and install Windows 7. But where’s the fun in that? Everything boring must be automated, so here’s the way to make your life just a little less boring. 
  1. Grab your copy of Windows 7 and extract/copy the content of the install disk into a local directory (if it’s a ISO file, use a tool like 7-zip). For exemplification I’ll use ‘d:\installs\win7’ as the directory containing the extracted Win7.
  2. Download the ISO Windows Automated Installation Kit (AIK) for Windows 7. As the name implies, this one is needed for automating the installation of Win7.
  3. Extract the AIK files from the downloaded ISO file (or burn it to disk) and install it.
  4. Download a neat little tool called WIM2VHD (which stands for Windows Image to Virtual Hard Disk), which is the tool that will actually do the automation for us. 
The WIM2VHD download is a Windows Script File (.wsf), which you will run after you’ve found out the correct SKU on your Windows Install Media (WIM). A Windows 7 installation disk may contain one or more versions (or stock-keeping units a.k.a. SKUs), e.g. Home, Premium, and Ultimate. To be able to install from the installation source extracted to ‘d:\install\win7’, the automation tool needs to know which version you intend to install. And for that you need a tool called ImageX, which came with the AIK you just installed.
  1. Go to the ‘tools’ folder in the AIK install folder (defaults to ‘c:\program files\windows aik\tools’)
  2. Find the appropriate version of the ImageX (if you’re running on a 32-bit OS that would be in the ‘x86’-folder, for 64-bit it’s the ‘ia64’) and copy it to the same folder as the WIM2VHD script.
  3. Run ImageX to find out which SKU to install
  4. Then run the WIM2VHD script with the the path to the WIM and the desired SKU as params;
cscript wim2vhd.wsf /wim:d:\installs\win7 \sources\install.wim /sku:ultimate

The script will then start making you a brand new VHD in the same folder you’re running the script from (alternatively you can add a /vhd param to the command above to specify a path to the output vhd).

Some minutes later you’ll have a (almost) pre-installed VHD with Windows 7.