$imageAll =get-childitem "D:\Hyper-V\master image" #the path of the parent VHD images
# $imageAll [0]. FullName
$i =1
#list all of the parent VHD images
foreach ($image in $imageAll)
{
Write-host "Type of OS" $i ($image)
$i + +
}
$typeOS =read-host "Please Input the type of OS"
$ParerentPath = $imageAll [$typeos-1]. FullName
$vmName =read-host "Please Input the VM Name"
$k =1
while ($k-eq 1)
{
if (GET-VM |? name-eq $vmname)
{
echo "The VM existed!"
$vmName =read-host "Please Input the VM Name"
$k =1
}
else {$k = 0}
}
$vhdpath = "D:\Hyper-V\Virtual hard disks\" + $vmName + ". Vhdx"
# $vhdsize = 300GB
New-vhd–parentpath $ParerentPath –path $vhdpath-differencing #create diffencing VHD
$switch = "Inside" #set VM connecting to Inside Vswitch
#create New VM
New-vm–name $vmName-generation 2–memorystartupbytes 1gb–vhdpath $vhdpath-switchname $switch
#setting the VM
SET-VM $vmName-dynamicmemory-memorymaximumbytes 4gb-automaticstartaction Nothing
Hyper-V PowerShell creates a virtual machine based on a differencing disk