Powershell – Automatically Create virtual Networking in Hyper-V

I’ve rebuilt my test lab a number of times and have gotten sick of setting up my NICs and VLANs, etc over and over in Hyper-V.  This Powershell script will build a virtual switch for each network device on your host system, binding physical adapters and needed, and then adds each VM to each switch.  Easy peasy!

Function Build-Networks{
$NetworkAdapters = Get-NetAdapter
Get-NetAdapter | % {New-VMSwitch -Name ($_.Name + " External Connection") -NetAdapterInterfaceDescription $_.InterfaceDescription}


Get-VM | % {$vm=$_.Name; Get-VMSwitch | % {Add-VMNetworkAdapter -ComputerName $vm -SwitchName $_.Name}} # add one nic per switch
Get-VM | Get-VMNetworkAdapter | ? SwitchName -eq $null | Remove-VMNetworkAdapter # remove null nic

Have a code issue? Share your code by going to Gist.github.com and pasting your code there, then post the link here!

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.