$p_w_picpathAll=Get-ChildItem “D:\Hyper-V\master p_w_picpath” #the path of the parent vhd p_w_picpaths
#$p_w_picpathAll[0].FullName
$i=1
#list all of the parent vhd p_w_picpaths
foreach ($p_w_picpath in $p_w_picpathAll)
{
Write-Host "Type of OS" $i ($p_w_picpath)
$i++
}
$typeOS=read-host "Please Input the type of OS"
$ParerentPath =$p_w_picpathAll[$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