PoshCode Logo PowerShell Code Repository

New-HyperVVM (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/571"></script>download | new post

Uses WMI to create a new virtual machine
inspired by “The Virtual PC Guy” http://blogs.msdn.com/virtual_pc_guy/archive/2008/05/28/scripting-vm-creation-with-hyper-v.aspx
Added the ability to specify a location to store the Virtual Machine
Also added remote capability to create a VM on a remote HyperV Host
Andy Schneider, http://www.get-powershell.com

  1. function New-HyperVVM {
  2.         param   (
  3.                         [string]$Hypervhost = "localhost",
  4.                         [string]$Vm = "VM Courtesy of PowerShell",
  5.                         [string]$location = "C:\MyVirtualMachines\$vm"
  6.                         )
  7.         $wmiClassString = "\\" + $Hypervhost + "\root\virtualization:Msvm_VirtualSystemGlobalSettingData"
  8.         $wmiclass = [WMIClass]$wmiClassString
  9.         $newVSGlobalSettingData = $wmiClass.CreateInstance()
  10.         $newVSGlobalSettingData.psbase.Properties.Item("ExternalDataRoot").value = $location
  11.         $newVSGlobalSettingData.psbase.Properties.Item("ElementName").value = $Vm
  12.         $VSManagementService = gwmi MSVM_VirtualSystemManagementService -namespace "root\virtualization" -ComputerName $Hypervhost
  13.         $GlobalSettings  = $newVSGlobalSettingData.psbase.GetText(1)
  14.         $VSManagementService.DefineVirtualSystem($GlobalSettings, $ResourceSettings)
  15. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me