Use WMI to create a VM with PowerShell

Posted On 2017-12-17 by ASchneider
Tags: Powershell Scripting HyperV Windows
Views: 105

A function to create a new Hyper-V virtual machine using WMI, with the ability to create the VM on a remote host. Also allows for specifiying the target location.
function New-HyperVVM {  param (
   [string]$Hypervhost = "localhost",
   [string]$Vm = "VM Courtesy of PowerShell",
   [string]$location = "C:\MyVirtualMachines\$vm"
 $wmiClassString = "\\" + $Hypervhost + "\root\virtualization:Msvm_VirtualSystemGlobalSettingData"
 $wmiclass = [WMIClass]$wmiClassString
 $newVSGlobalSettingData = $wmiClass.CreateInstance()
 $newVSGlobalSettingData.psbase.Properties.Item("ExternalDataRoot").value = $location
 $newVSGlobalSettingData.psbase.Properties.Item("ElementName").value = $Vm
 $VSManagementService = gwmi MSVM_VirtualSystemManagementService -namespace "root\virtualization" -ComputerName $Hypervhost
 $GlobalSettings  = $newVSGlobalSettingData.psbase.GetText(1)
 $VSManagementService.DefineVirtualSystem($GlobalSettings, $ResourceSettings)

About the Author

has posted a total of 1 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links