Managing the Default Virtual Machine Location with Hyper-V

After my post about managing the default virtual machine location with Windows Virtual PC some people have been asking me about how Hyper-V handles default virtual machine locations.

Hyper-V has two default locations that you can configure. 

The first is the default virtual hard disk location – which is set to “\Users\Public\Documents\Virtual Hard Disks” on the system disk by default.

The second is the default location for virtual machine configuration files – which is set to “\ProgramData\Microsoft\Windows\Virtualization” on the system disk by default.

Both of these settings can be changed by accessing the Hyper-V server settings (from the Hyper-V Manager):

image

I have also put together a script to show you how to set these settings programmatically:

 Function ProcessResult($result, $successString, $failureString)
 {
    #Return success if the return value is "0"
    if ($result.ReturnValue -eq 0)
       {write-host $successString} 
  
    #If the return value is not "0" or "4096" then the operation failed
    ElseIf ($result.ReturnValue -ne 4096)
       {write-host $failureString "  Error value:" $result.ReturnValue}
  
    Else
       {#Get the job object
       $job=[WMI]$result.job
  
       #Provide updates if the jobstate is "3" (starting) or "4" (running)
       while ($job.JobState -eq 3 -or $job.JobState -eq 4)
          {write-host $job.PercentComplete "% complete"
           start-sleep 1
  
           #Refresh the job object
           $job=[WMI]$result.job}
  
        #A jobstate of "7" means success
        if ($job.JobState -eq 7)
           {write-host $successString}
        Else
           {write-host $failureString
           write-host "ErrorCode:" $job.ErrorCode
           write-host "ErrorDescription" $job.ErrorDescription}
        }
 }
  
 # Prompt for the Hyper-V Server to use
 $HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
  
 # Get the management service
 $vmms = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
  
 # Get the management service settings
 $vmmsSettings = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementServiceSettingData -computername $HyperVServer
  
 # Display the information about the current default paths
 Write-host "The default virtual machine configuration path is currently:"
 write-host "    - " $vmmsSettings.DefaultExternalDataRoot
 write-host
 Write-host "The default virtual hard disk path is currently:"
 write-host "    - " $vmmsSettings.DefaultVirtualHardDiskPath
 write-host
  
 # Prompt for the new default virtual machine configuration path
 $vmmsSettings.DefaultExternalDataRoot = Read-Host "Specify the new default virtual machine configuration path"
  
 # Prompt for the new default virtual hard disk path
 $vmmsSettings.DefaultVirtualHardDiskPath = Read-Host "Specify the new default virtual hard disk path"
  
 # Update the settings
 $result = $vmms.ModifyServiceSettings($vmmsSettings.GetText(1))
  
 # Process the results
 ProcessResult $result "Set new default paths." "Failed to set new default paths."

 

Cheers,
Ben

SetHyperVPaths.zip