Q: How can I configure my virtual machine (VM) template to set the product key automatically?

A: If you're using VM templates, the easiest way to specify the product key is by using something like System Center 2012 Virtual Machine Manager, which can inject whatever product key you need as part of the template. However, if you want to configure the product key outside of a management solution, you can use an answer file.

Below is a very basic answer file that specifies the Key Management Service (KMS) key for Windows Server 2012 Datacenter:

<?xml version="1.0" encoding="utf-8"?>
  <unattend xmlns="urn:schemas-microsoft-com:unattend">
  <settings pass="windowsPE">
    <component name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <UserData>
      <ProductKey>
        <Key>48HP8-DN98B-MYWDG-T2DCC-8W83P</Key>
        <WillShowUI>OnError</WillShowUI>
      </ProductKey>
      <AcceptEula>true</AcceptEula>
    </UserData>
  </component>
  </settings>
</unattend>

I save this as unattend.xml in a folder on the machine that will be SYSPREP'd as c:\unattend\unattend.xml.

I then run sysprep, passing this file. For example:

c:\windows\system32\sysprep\sysprep /generalize /shutdown /oobe /unattend:c:\unattend\unattend.xml

When the template is used, there will be no prompt for the product key. In the past, keys were set as part of the specialist phase, for example:

 <settings pass="specialize">
  <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ProductKey>48HP8-DN98B-MYWDG-T2DCC-8W83P</ProductKey>
  </component>
</settings>