Windows Sysprep for Azure Virtual Machines

Sysprep is a tool to capture the state of a Windows machine and reuse this as an image to create other Virtual Machines quickly from. The following article is intended to be used to create an image from an Azure Virtual machine to create other machines quickly.

Once the virtual machine is in a state to be imaged, follow these steps to Sysprep the machine.

Generalize a VM before creating an image – Azure Virtual Machines | Microsoft Docs

  1. Create a backup of the VM, as after the VM is Sysprep’d you cannot go back and make future changes. This will allow you to restore from a backup to maintain your image going forward. Wait until the backup completes before moving onto the next step.
  2. Create a new Virtual Machine from the backup that was just created. Go to the virtual machine, and choose Backup from the Operations section.
    1. Choose Restore VM to create the new virtual machine.

    1. Choose a restore point.
    2. Create the new Virtual Machine.

Graphical user interface, text, application

Description automatically generated

NOTE: Applications that were installed with per-user context will now cause sysprep to fail. An example application that will cause sysprep to fail is the Edge browser if used during the configuration of the machine. Follow the instructions below to remove the app before continuing with sysprep instructions.

Steps to create the Sysprep Image:

  1. Once the new virtual machine is created, sign into your new Windows VM.
  2. Open a Command Prompt window as an administrator.
  3. Delete the panther directory (C:\Windows\Panther).
  4. Then change the directory to %windir%\system32\sysprep, and then run:
  5. c:\windows\system32\sysprep\sysprep.exe /oobe /generalize /shutdown /mode:vm
    1. Note: The /mode:vm switch seemed to address that other issue where in virtual machines view the VM would show up as updating… Now it shows updating for short period of time but then goes to Stopped.
  6. Go to the Azure Portal to view the virtual machine that is being imaged to gather the Virtual Machine name, the resource group name, and subscription ID.
  7. Open Cloud Shell or log into Azure on your PC locally.
    1. It is a good practice to set the subscription that you are working on.
      1. az account set –subscription “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”
  8. Then run this to tag the VM as generalized
    1. set-azvm -resourcegroupname XXXresourcegroupnameXXX -name XXXvmnameXXX -generalized
  9. If you would try to start the virtual machine it would show this message.

Graphical user interface, text

Description automatically generated

  1. This command may also be used to check to see if a virtual machine is generalized or not.
    1. get-azvm -resourcegroupname XXXresourcegroupnameXXX -name XXXvmnameXXX -status
  2. After the Virtual Machine is finished with the Sysprep command the virtual machine should go to a Stopped state. Once the status has changed to stopped. Stop the virtual machine in the Azure portal to deallocate the VM. This will stop the VM from being billed for compute.
  3. At this point the virtual machine may be captured to an image. Follow this article for the image capture process.
    1. Capture an image of a VM using the portal – Azure Virtual Machines | Microsoft Docs

Note: There are other Sysprep articles intended for on-premises Virtual Machines. This refers to using the GUI version of Sysprep and command line syntax. It was determined that using these options on some SKU’s of VM’s did not generalize the Azure machines properly which caused failures after images were created causing timeouts during image deployments. https://docs.microsoft.com/en-us/azure/virtual-machines/windows/upload-generalized-managed

It is not recommended to use the above link for Azure Virtual Machines Sysprep process.

Other Sysprep Troubleshooting

Microsoft Store Apps cause Sysprep to fail – (ex: installing Edge Updates). This link describes issues when software is installed per user. It is unable to clean the application up and must be done manually.

Sysprep fails with Microsoft Store apps – Windows Client | Microsoft Learn

Registry Errors

It was determined that some Windows Server SKU’s were missing a Registry entry causing Sysprep failures.

Use Sysprep log files to look at errors. If you see this error in the log continue to the step 2.

  1. Look at the log file: C:\windows\System32\Sysprep\Panther\setuperr.log
    1. Error [0x0f0082] SYSPRP LaunchDll:Failure occurred while executing ‘DscCore.dll,SysPrep_Cleanup’, returned error code 0x2
  2. Set-ItemProperty -Path “HKLM:\SOFTWARE\Microsoft\DesiredStateConfiguration” -Name “AgentId” -Value “”
    1. This command was used if the value was not able to be able to be set properly with the reg add command noted in (a.)

Sysprep errors

  • User received the Error Handler message during sysprep.
  • User received the error: ‘A fatal error occurred while trying to sysprep the machine’

The article below references to checking several registry entries.

Fix: A Fatal Error Occurred While Trying to Sysprep the Machine – Appuals.com

Make sure to backup your registry before making any changes.

Check the registry to see if Sysprep has been ran multiple times, and correct the values.

  • KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform\
    • Change this value to 1
  • HKEY_LOCAL_MACHINE\SYSTEM\Setup\Status\SysprepStatus
    • Change CleanupState to 2 and GeneralizationState to 7

If the above do not fix the sysprep issue try this command below. Verify that the registry entries are correct before trying the next command.

  • msdtc -uninstall
  • msdtc -install

About the author

Jeff Pigott is a Senior Cloud Solution Architect at Microsoft, specializing in Azure Infrastructure technologies including Azure Arc, Azure Update Manager, SQL DB, security, and storage. With over 29 years of experience in IT, development, and DevOps, Jeff has worked with a wide range of platforms, tools, and languages. Jeff is passionate about staying up-to-date with the latest Microsoft technologies and services, and holds multiple Azure certifications.