Creating a Hyper-V Network Switch with Windows PowerShell

Today I was playing around with Hyper-V and needed to create an External Virtual Network Switch. Here’s a sample bit of code showing how you can do this.

# Use Get-NetAdapter to find the name of your network adapters
$EthernetAdapter = Get-NetAdapter -Name “Local Area Connection”

# Name of the Virtual Network Switch
$VMNetworkSwitch = “External Lab”

# Test if the Switch is already present, if not then create it
$TestSwitch = Get-VMSwitch -Name $VMNetworkSwitch -ErrorAction SilentlyContinue; if ($TestSwitch.Count -eq 0){Write-Host “The External Network Switch” `”$VMNetworkSwitch`” “has not been found. Creating switch”; New-VMSwitch -Name “External Lab” -NetAdapterName $EthernetAdapter.Name -AllowManagementOS $true -Notes “External Network Access for My Lab”} else {Write-Host “The External Network switch” `”$VMNetworkSwitch`” “already exists”}


No comments yet.

Leave a Reply