PowerShell Hyper-V networking cmdlets (1/3)
Hyper-V networking is how VMs on a Windows host get network access: virtual switches (External, Internal, Private), virtual NICs on those switches, and port ACLs that filter traffic at the switch port. This first part covers the Add-, Connect-/Disconnect-, Get- and extension cmdlets, and a Linux lab rebuilds the same idea with a bridge, namespaces, veth and nftables.
260.1 Why is it used?
- Every VM needs a virtual NIC plugged into a virtual switch to talk to anything.
- The switch type decides reach: LAN, host only, or VMs only.
- Port ACLs isolate or meter VM traffic without touching the guest OS.
- Runs in Windows PowerShell and PowerShell 7 on Windows Server / Windows 10-11 with Hyper-V; New/Set/Remove need Administrator.
- Office PC: Hyper-V module not installed (Count 0), so syntax is from Microsoft Learn and the lab is on Linux.
Get-Command -Module Hyper-V ` -ErrorAction SilentlyContinue | Measure-ObjectPS C:\> Get-Command -Module Hyper-V -ErrorAction SilentlyContinue | Measure-Object Count : 0260.2 How it works: three switch types
- Step 1
1.A vSwitch is a software switch inside the Hyper-V host where VM vNICs connect
- Step 2
2.External: bound to the physical NIC Ethernet; VM1 traffic reaches the office LAN directly
- Step 3
3.With AllowManagementOS the host also uses this switch through a vEthernet adapter (172.16.0.1)
- Step 4
4.Internal: no physical NIC; VM3 and the host talk, nothing goes to the LAN (best for a lab, with NAT)
- Step 5
5.Private: VM4 and VM5 talk only to each other; even the host cannot see them
| Switch type | VM to VM | VM to host | VM to LAN | Typical use |
|---|---|---|---|---|
| External | Yes | Yes (with AllowManagementOS) | Yes | Production VMs, internet |
| Internal | Yes | Yes | No (only via NAT) | Test lab with host access |
| Private | Yes | No | No | Isolated cluster / test |
260.3 Add-VMSwitch, team members, Add-VMNetworkAdapter
- Common parameters here: -CimSession (remote), -Confirm, -WhatIf.
- Add-VMSwitch does NOT create a switch: it adds an existing switch to an Ethernet resource pool. New-VMSwitch creates one (next video).
- Add-VMSwitchTeamMember adds physical adapters (-NetAdapterName) to a switch team; Get-VMNetworkAdapterTeamMapping shows the mapping.
- Add-VMNetworkAdapter adds a vNIC to a VM; -SwitchName connects it, -ManagementOS makes a host vNIC.
- -StaticMacAddress / -DynamicMacAddress decide the MAC; -IsLegacy $true = legacy (boot) NIC.
Add-VMSwitch -Name Test -ResourcePoolName "Engineering Department"$VMSwitch = Get-VMSwitch -Name "Switch03"Add-VMSwitchTeamMember -VMSwitch $VMSwitch -NetAdapterName "Adapter01","Adapter04"Get-VMNetworkAdapterTeamMapping -VMName VM1 Add-VMNetworkAdapter -VMName Redmond -Name "Redmond NIC1"Add-VMNetworkAdapter -VMName Test -SwitchName NetworkGet-VM Test | Add-VMNetworkAdapter -IsLegacy $true -Name BootableAdd-VMNetworkAdapter -ManagementOS -Name Secondary260.4 Connect-VMNetworkAdapter and Disconnect-VMNetworkAdapter
- Connect attaches an adapter to a switch (-SwitchName or -VMSwitch via pipeline).
- -UseAutomaticConnection connects to any switch of the resource pool.
- Disconnect = pulling the cable: the adapter stays in the VM but is on no switch.
- Pipelines work: Get-VMNetworkAdapter | Where-Object | Disconnect-VMNetworkAdapter.
Connect-VMNetworkAdapter -VMName Test1,Test2 -Name Internet -SwitchName InternetAccessGet-VMNetworkAdapter -VMName Test1 | Connect-VMNetworkAdapter -SwitchName InternetAccessGet-VMSwitch InternetAccess | Connect-VMNetworkAdapter -VMName Test1 Disconnect-VMNetworkAdapter -VMNetworkAdapter Test1Get-VMNetworkAdapter -VMName * | Where-Object {$_.SwitchName -eq 'InternetAccess'} | Disconnect-VMNetworkAdapter260.5 Port ACL and extended ACL
- Step 1
1.Every VM adapter connects to a port of vswitch0
- Step 2
2.A port ACL can Allow, Deny or Meter traffic by IP or MAC
- Step 3
3.VM1 (.11) pings VM2 (.12): the ICMP echo passes the switch
- Step 4
4.VM2's port has a Deny rule for ICMP towards VM1
- Step 5
5.ICMP from VM2 is dropped right at the switch port
| Point | Add-VMNetworkAdapterAcl | Add-VMNetworkAdapterExtendedAcl |
|---|---|---|
| Matches on | Remote/local IP and MAC | IP + protocol + ports |
| Actions | Allow | Deny | Meter | Allow | Deny |
| Direction | Inbound | Outbound | Both | Inbound | Outbound |
| Order | Longest match wins in the same direction | Larger -Weight applies first; after a match the rest is ignored |
| Extras | ANY = IPv4 + IPv6; new adapter has no ACL | -Stateful $True (return packets), -IdleSessionTimeout, -IsolationID; Protocol 1 = ICMP |
Add-VMNetworkAdapterAcl -VMName Redmond -RemoteIPAddress 10.0.0.0/8 -Direction Both -Action AllowAdd-VMNetworkAdapterAcl -VMName Redmond -RemoteIPAddress ANY -Direction Both -Action DenyGet-VM Redmond | Add-VMNetworkAdapterAcl -RemoteMacAddress 03-0f-01-0e-aa-b2 -Direction Both -Action DenyGet-VMNetworkAdapter -VMName Redmond | Add-VMNetworkAdapterAcl -RemoteIPAddress 192.168.0.0/16 -Direction Outbound -Action Meter # allow RDP (3389) inbound, statefulAdd-VMNetworkAdapterExtendedAcl -VMName "TSQA01" -Action Allow -Direction Inbound ` -LocalPort "3389" -Protocol "TCP" -Weight 10 -Stateful $True260.6 Get- cmdlets, routing domains and switch extensions
| Cmdlet | What it shows / does |
|---|---|
| Get-VMNetworkAdapter | Adapters of VMs (-VMName *), the host (-ManagementOS) or both (-All); also snapshots |
| Get-VMNetworkAdapterAcl | Port ACLs; a Both rule appears in the Inbound and Outbound lists |
| Get-VMNetworkAdapterExtendedAcl | Extended ACL entries |
| Get-VMNetworkAdapterFailoverConfiguration | IP config used after a failover |
| Add-/Get-VMNetworkAdapterRoutingDomainMapping | Tenant routing domain (GUID) + virtual subnet (IsolationID) on a gateway VM NIC |
| Get-VMNetworkAdapterIsolation | Isolation method and multitenancy mode |
| Enable-/Disable-VMSwitchExtension | Turn an extension (e.g. Windows Filtering Platform) on/off on a switch |
| Add-VMSwitchExtensionPortFeature / SwitchFeature | Feature on one adapter (DHCP guard, router guard) / whole switch (bandwidth) |
Get-VMNetworkAdapter -AllGet-VMNetworkAdapterAcl -VMName RedmondGet-VMNetworkAdapterFailoverConfiguration VM01 -VMNetworkAdapterName NetworkAdapter Add-VMNetworkAdapterRoutingDomainMapping -VMName "Gateway01" ` -VMNetworkAdapterName "Internal NIC" ` -RoutingDomainID "{5a07361e-6a54-49fc-9210-bfbf14a5c56f}" ` -RoutingDomainName "Contoso" -IsolationID 6000 -IsolationName "ContosoGatewayVsid"Get-VMNetworkAdapterIsolation -VMName Multitenant-GW -VMNetworkAdapterName InternalNIC $feature = Get-VMSwitchExtensionSwitchFeature -FeatureName "Ethernet Switch BandwidthSettings"$feature.SettingData.DefaultFlowReservation = 300000000Add-VMSwitchExtensionSwitchFeature "External" -VMSwitchExtensionSwitchFeature $featureEnable-VMSwitchExtension -VMSwitchName External -Name "Microsoft Windows Filtering Platform"260.7 Lab: bridge + netns + veth + nftables
| Hyper-V | Linux equivalent |
|---|---|
| Virtual switch (no uplink = Private) | ip link add vswitch0 type bridge |
| VM | Network namespace (ip netns add vm1) |
| VM network adapter + Connect | veth pair, port end on the bridge (master vswitch0) |
| Disconnect-VMNetworkAdapter | ip link set vm2-port nomaster |
| Get-VMNetworkAdapter | ip -br link / bridge link |
| Port ACL | nftables netdev ingress hook on vm2-port |
ip link add vswitch0 type bridgeip link set vswitch0 upip netns add vm1ip link add vm1-port type veth peer name eth0 netns vm1ip link set vm1-port master vswitch0ip link set vm1-port upip -n vm1 addr add 192.168.60.11/24 dev eth0ip -n vm1 link set eth0 up# vm2: same steps, 192.168.60.12/24 bridge linkip netns exec vm1 ping -c 3 -W 1 192.168.60.12ip link set vm2-port nomaster # = Disconnectip link set vm2-port master vswitch0 # = Connect[hvhost]$ bridge link4: vm1-port@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master vswitch0 state forwarding priority 32 cost 25: vm2-port@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master vswitch0 state forwarding priority 32 cost 2[hvhost]$ ip netns exec vm1 ping -c 3 -W 1 192.168.60.12PING 192.168.60.12 (192.168.60.12) 56(84) bytes of data.64 bytes from 192.168.60.12: icmp_seq=1 ttl=64 time=0.198 ms64 bytes from 192.168.60.12: icmp_seq=2 ttl=64 time=0.122 ms64 bytes from 192.168.60.12: icmp_seq=3 ttl=64 time=0.132 ms3 packets transmitted, 3 received, 0% packet loss, time 2046ms- After nomaster only vm1-port is left in bridge link and the ping gives 100% loss; after master vswitch0 it works again. The adapter was never deleted.
- The kernel has no nftables bridge family, so the ACL is a netdev ingress hook on vm2-port (only built-in expressions; the 22 kernel modules stayed the same).
nft add table netdev portaclnft add chain netdev portacl vm2port \ '{ type filter hook ingress device vm2-port priority 0; }'nft add rule netdev portacl vm2port \ ip saddr 192.168.60.12 ip daddr 192.168.60.11 ip protocol icmp counter dropip netns exec vm2 ping -c 3 -W 1 192.168.60.11ip netns exec vm1 ping -c 3 -W 1 192.168.60.12nft list table netdev portaclnft delete table netdev portacl[hvhost]$ ip netns exec vm2 ping -c 3 -W 1 192.168.60.11--- 192.168.60.11 ping statistics ---3 packets transmitted, 0 received, 100% packet loss, time 2048ms[hvhost]$ nft delete table netdev portacl(exit 0)[hvhost]$ nft list tablestable ip nat[hvhost]$ ip netns exec vm2 ping -c 3 -W 1 192.168.60.1164 bytes from 192.168.60.11: icmp_seq=1 ttl=64 time=0.102 ms64 bytes from 192.168.60.11: icmp_seq=2 ttl=64 time=0.223 ms64 bytes from 192.168.60.11: icmp_seq=3 ttl=64 time=0.171 ms3 packets transmitted, 3 received, 0% packet loss, time 2042ms- The ping from vm1 also failed: vm2's echo replies are ICMP from .12 to .11 and hit the same rule; the counter showed 6 packets (3 requests + 3 replies).
260.8 Common mistakes
- Thinking a one-way ICMP deny blocks only one ping: replies match too; plan both directions or use -Stateful in the extended ACL.
- Using Add-VMSwitch to create a switch: it only adds to a resource pool; use New-VMSwitch.
- Picking Private for a lab that needs host access (the host cannot see Private VMs), or External when nothing should reach the LAN.
- Expecting Disconnect-VMNetworkAdapter to remove the NIC: it stays in the VM.
260.9 Interview tips
- External = bound to a physical NIC (LAN + internet); Internal = VMs + host only; Private = VMs only.
- Lab where VMs talk to each other and the host but not the LAN: Internal switch.
- Port ACL = IP/MAC with Allow/Deny/Meter; extended ACL = ports, weight and stateful rules.
- Map it to Linux: bridge = vSwitch, netns = VM, veth = vNIC, nftables = port ACL.