Note: This article applies equally to the server-side and client-side hypervisor. Although some scenarios may not apply to older versions, the technical steps apply to all versions of Hyper-V released to date.
A virtual machine in Hyper-V consists of a few files that account for its virtual hardware configuration and the virtual storage (VHD and VHDX files). By default the virtual machine configuration files are stored in C:\ProgramData\Microsoft\Windows\Hyper-V, and the virtual hard drives are stored in C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks.
One slight improvement in Hyper-V (in Windows Server 2012) is that during the installation process (GUI mode only) it gives you the option of changing these defaults. However the defaults are still the same as they used to be… on the C drive.
While you can change these file locations on a per-VM basis when you create them, there are several reasons why you might want to change these global default locations within your hosts.
- If you enable cluster shared volumes (CSVs) then the best practice would be to store your new VMs in the CSV to facilitate live migration or failover.
- In the client-side hypervisor in Windows 8 installed on a laptop you may want to use an external drive connected to a fast USB port to store your virtual machines.
- For our demo and lab machines, we find it easier to store the VHD files in the root of the C drive for easier navigation.
Whatever the reason, here is a simple way to make it happen. Remember, these steps will apply to virtual machines created going forward only, and will not move any pre-existing VMs.
- In the global Actions pane of Hyper-V Manager click Hyper-V Settings…
- Under Virtual Hard Disks change the location from the default to your desired location.
- Under Virtual Machines change the location from the default to your desired location, and click apply.
4. Click OK to close the Hyper-V Settings page.
PowerShell Tip: I am often asked ‘Can you do this in PowerShell?’ My usual reply is that PowerShell can do almost anything. So thanks to my #1 PowerShell Resource (PowerShell MVP Sean Kearney) I can provide you with the exact script to accomplish the same task. This method has the added benefit of working on either local or remote Hyper-V hosts, assuming you have the proper credentials.
- Make sure the directories you plan to use are already created.
- SET-VMHOST –computername <server> –virtualharddiskpath ‘C:\VHDs'
- SET-VMHOST –computername <server> –virtualmachinepath ‘C:\VMs’
By the way, you can also string these two together with the following script:
SET-VMHOST –computername <server> -virtualharddiskpath 'C:\VHD' -virtualmachinepath 'C:\VM'
Now when you create a new virtual machine, the files will be stored where you want them, without having to change them manually. It’s simple!