Share via


Hyper-V: Export VM Config-Only Using PowerShell

Read about configuration-only (config-only) export of your VMs on this blog post: http://blogs.technet.com/b/virtualization/archive/2009/05/29/hyper-v-r2-import-export-part-6-so-what-happened-to-configuration-only-export.aspx

Here is a sample PowerShell script to export VM config-only:

$ns = 'root\virtualization\v2'
$expDir = 'd:\vmexports'
 
#Get VM Object
$vm = gwmi -n $ns Msvm_ComputerSystem | ?{$_.ElementName -eq 'VM'}  #VM Name here to change
 
#Get export setting object
$exp = @($vm.GetRelated('Msvm_VirtualSystemExportSettingData'))[0]
 
#If you dont want to copy the VHDs and AVHDs
$exp.CopyVmStorage = $false
 
#If you dont want to copy the Saved state
$exp.CopyVmRuntimeInformation = $false
 
#Get VMMS object
$vmms = gwmi -n $ns Msvm_VirtualSystemManagementService
 
#Perform Export
$out = $vmms.ExportSystemDefinition($vm.Path.Path, $expDir, $exp.GetText(1))
 
#Perform Job handling if necessary
if ($out.ReturnValue -eq 4096)
{
       $task = [Wmi]$out.Job;
       while($task.JobState -eq 3 -or $task.JobState -eq 4)
       {
              $task.Get();
              sleep 1;
       }
       if ($task.JobState -ne 7)
       {
              "Error exporting VM " + $task.ErrorDescription;
       }            
       else
       {
              "Export completed successfully..."
       }     
      
}
elseif ($out.ReturnValue -ne 0)
{
       "Export failed with error : " + $out.ReturnValue;
}
else
{
       "Export completed successfully..."
}