Create a Hyper-V Machine with one click

Hey there!

If you working with Client Management, you probably have some test-pc´s on your machine, Hyper-V machines maybe?

So Ive made a script that creates Hyper-V machines pretty sweet actually.

Let me go through the script here.

1. First, you need to modify the script, Go to the #Modify part of the script, change it so it fits your computer.

2 If you dont know what NetworkSwitch that you uses, you can find it Here
(In my case im using “Bridge”
2018-01-23 11_30_39-Hyper-V Manager.png
2. Then save it and run the script.


Param(
[Parameter(mandatory=$True,HelpMessage="Whats the name of the VM?")]
[ValidateNotNullOrEmpty()]
$VM_Name,

[parameter(mandatory=$True,HelpMessage="How much memory do you want in GB?(Example 4)")]
[ValidateNotNullOrEmpty()]
$VMMemoryinGB,

[parameter(mandatory=$True,HelpMessage="How large HD in GB (Example 80)")]
[ValidateNotNullOrEmpty()]
$HDsizeinGB
)

Function Select-FolderDialog
{
    param([string]$Description="Select a Folder that you wanna save your vhdx files",[string]$RootFolder="Desktop")

 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
     Out-Null     

   $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
        $objForm.Rootfolder = $RootFolder
        $objForm.Description = $Description
        $Show = $objForm.ShowDialog()
        If ($Show -eq "OK")
        {
            Return $objForm.SelectedPath
        }
        Else
        {
            Write-Error "Operation cancelled by user."
        }
    }

# You can modify here if you always want to save your machines to a specific folder.
$Folderpath = Select-FolderDialog
$Path = "$Folderpath\$VM_name.vhdx"

#Modify
#Change $NetworkSwitch to your standard switch in hyper-v, mine is Bridge
$NetworkSwitch = "Bridge"

#Creating Virtual machine
New-VM -Name "$VM_Name" –MemoryStartupBytes ([int64]$VMMemoryinGB*1024*1024*1024) -NewVHDPath $Path -NewVHDSizeBytes ([int64]$HDsizeinGB*1024*1024*1024) -Generation 2 -SwitchName $NetworkSwitch

#Change boot Order 'Network THEN Hardrive'
$vmNetworkAdapter = get-VMNetworkAdapter -Name "Network Adapter" -VMName "$VM_Name"
$vmHardDiskDrive = get-VMHardDiskDrive -VMName "$VM_Name"
Set-VMFirmware "$VM_Name" -BootOrder $vmNetworkAdapter, $vmHardDiskDrive

Start-vm $VM_name

& vmconnect.exe localhost $VM_name

Write-host "Succefully Created VM, Starting.."  -ForegroundColor Green

( You can also download the script here New-VM )

3. When you running the script, you gotta answer some questions. (Type !? for help)

4 (Optional) If you want you could just run the script with some parameters like this or you could run the script in the console
2018-01-22 10_41_34-Administrator_ Windows PowerShell ISE.png

5. Answer all of them like this.
2018-01-22 10_27_28-Administrator_ Windows PowerShell ISE.png

6. Then the Machine will start, and you are good to go.
2018-01-22 10_28_49-Edit Post ‹ PONTUSWENDT.BLOG - Client Management — WordPress.com.png

7. Done

Good luck

/Pontus

One thought on “Create a Hyper-V Machine with one click

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s