Powershell - Automatically Create virtual Networking in Hyper-V

Published September 20, 2013 by FoxDeploy

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
    } 
}

Build-Networks

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

Microsoft MVP

Five time Microsoft MVP, and now I work for the mothership


Need Help?

Get help much faster on our new dedicated Subreddit!

depicts a crowd of people in a night club with colored lights and says 'join the foxdeploy subrreddit today'


Blog Series
series_sml_IntroToDsc
series_sml_PowerShellGUI series_sml_IntroToRaspberryPi Programming series_sml_IntroToWindows Remote Management Series The Logo for System Center Configuration Manager is displayed here Depicts a road sign saying 'Learning PowerShell Autocomplete'




Blog Stats