3

I am writing a PowerShell script to clone existing hyper-v VMs/ create new VMs based on a template. Where ever I look the cmdlets Export-VM and Import-VM are used. However the cmdlet New-VM has the option to use an existing VHD aswell. Wouldn't just copying the VHD and creating a New-VM be easier? It's just 1 file instead of VHD + Virtual Machine Folder + setting a new Snapshot folder. (The Template has no Snapshots)

What are the advantages of using Import-VM over New-VM? Are there any or is this primary opinion based?

T-Me
  • 163
  • 4

1 Answers1

6

A VM consists of two different parts

  1. The Virtual HardDisk files (one or multiple) that hold the OS and data
  2. The configuration of the VM within Hyper-V, including the virtualized hardware available to the VM.

If you just copy a VHDX file and use New-VM you only get the first part of the original VM. Sure you can try to use parameters on New-VM to make the new VM similar to the original one, but you may miss something.

Using Export-VM and Import-VM you copy both parts of the VM.

Peter Hahndorf
  • 14,058
  • 3
  • 41
  • 58
  • 1
    So if I want to set the CPU and RAM in the Script `New-VM` might indeed be perferable? I guess software registrations might get invalid by `New-VM` due to new Hardware IDs but might stay valid with `Import-VM -GenerateNewId` because it changes just the ID of the VM for Hyper-V and the MAC? – T-Me Apr 08 '20 at 07:12
  • 1
    @T-Me yes, and if you copy the VM to another host, keeping the original ID can be helpful. – Peter Hahndorf Apr 08 '20 at 11:03