Q

Use your PowerShell prowess to add VHD files to a Hyper-V VM

Need to import VHD files into Hyper-V, but don't know how to go about it? PowerShell to the rescue!

How do I import VHD files into Hyper-V?

Whether you want to add VHD files to a new virtual machine or import one from an exported VM, you can accomplish the tasks quickly and easily with PowerShell and the Hyper-V cmdlets.

If you have properly exported your VM using the graphical Hyper-V manager or PowerShell, retaining all memory and network settings, you will just as easily be able to import the VM back into Hyper-V.

The following shows an example of exporting a VM to disk, removing the VM from Hyper-V and importing the VM back into Hyper-V.

PS > Export-VM –Name Server1 –Path d:\

PS > Remove-VM –Name Server1

PS > Import-VM –Path ‘c:\Server1\Virtual Machines\BB75F3E6-44E0-4688-86

67-68C849F47D14.XML'

More resources on VHD files

Navigating VHD formats

Backing up VHD files for extra security

How to convert deployment images to VHD files

The –path parameter for the Import-VM cmdlet should be the .XML file under the Virtual Machines folder. This quickly imports the VM, which is ready to be started using the Start-VM command.

I often run into a situation where I have the .VHD (or .VHDX) file and I want to make a new VM in Hyper-V using this .VHD. This situation differs from importing a VM because you need to specify the network switch, as well as memory and processor settings.

The example below shows a VHD named Server1.vhdx. I create the new VM, add it to my existing switch named Inside and then set the memory settings.

PS > New-VM -Name Server1 -VHDPath C:\hyperv\Server1.vhdx -SwitchName (Get-VMSwitch -name inside* | Select-Object -ExpandProperty name)

PS > Set-VMMemory -VMName Server1 -DynamicMemoryEnabled $True -StartupBytes 1GB -MinimumBytes 512mb -MaximumBytes 2GB

PS > Start-VM -Name Server1

Using PowerShell to add or import VMs into Hyper-V allows you to tackle many management problems. You could even take this process a step further and automate the process of migrating several VMs from one host to another.

This was first published in April 2013
This Content Component encountered an error

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close