PowerShell NetworkSwitchManager module
The NetworkSwitchManager module manages a real physical network switch from PowerShell: its Ethernet ports, VLANs, port modes, features and saved configuration. Every command reaches the switch through a CimSession to the switch's OMI/CIM agent.
264.1 Why is it used?
- Automate switch work (ports, VLANs, features) from the same PowerShell scripts that manage servers.
- Standard DMTF profile, so one set of cmdlets works across vendors whose switches have an OMI/CIM agent.
- Built into Windows Server and Windows 10/11; the video's office PC counted 19 cmdlets.
PowerShell
Get-Command -Module NetworkSwitchManager -ErrorAction SilentlyContinue | Measure-ObjectOutput
PS C:\> Get-Command -Module NetworkSwitchManager -ErrorAction SilentlyContinue | Measure-Object Count : 19264.2 How it works
- Step 1
1.Admin PC: $Session = New-CimSession -ComputerName "NetworkSwitch08"
- Step 2
2.Request reaches the switch's OMI agent over WS-Man
- Step 3
3.Disable-NetworkSwitchEthernetPort -CimSession $Session -PortNumber 21
- Step 4
4.Switch turns port 21 off: that PC's link goes down
- Step 5
5.PC on port 5 keeps working normally
- Common parameters: -CimSession (which session/switch), -Confirm (ask first), -WhatIf (show only).
- Pick a port by -PortNumber, -DeviceId or an -InputObject from the pipeline.
264.3 Ports: Enable, Disable, Get
PowerShell
$Session = New-CimSession -ComputerName "NetworkSwitch08"Disable-NetworkSwitchEthernetPort -CimSession $Session -PortNumber 21Disable-NetworkSwitchEthernetPort -CimSession $Session -DeviceId "Ethernet17"Enable-NetworkSwitchEthernetPort -Port 21 -CimSession $SessionGet-NetworkSwitchEthernetPort -PortNumber 21 -CimSession $SessionOutput
Name PortNumber DeviceID FullDuplex AutoSense PortType MaxDataSize---- ---------- -------- ---------- --------- -------- -----------Ethernet17 21 Ethernet17 True False 0 9214- Get-NetworkSwitchEthernetPort with only -CimSession lists all ports; -FullDuplexEnabled / -FullDuplexDisabled filter by duplex.
264.4 VLANs
- Step 1
1.pc1 (VLAN 10) pings pc2 (VLAN 10)
- Step 2
2.switch1 forwards it within VLAN 10 -> reply
- Step 3
3.pc1 pings pc3 (VLAN 20), same subnet
- Step 4
4.Switch drops it: different VLAN = different LAN
Crossing VLANs needs a router / L3 port
| Cmdlet | Job | Key options |
|---|---|---|
| Get-NetworkSwitchVlan | List VLANs | -Name, -VlanId, -Caption, -Description, -InstanceId |
| New-NetworkSwitchVlan | Create a VLAN | -Name, -VlanID, -Caption, -Description |
| Enable- / Disable-NetworkSwitchVlan | Turn a VLAN on/off | -InstanceId, -Name, -VlanId |
| Remove-NetworkSwitchVlan | Delete VLANs (wildcards) | -VlanID, -Name "*Contoso*", -InputObject |
| Set-NetworkSwitchVlanProperty | Rename / describe | -Property @{ElementName=...; Description=...} |
PowerShell
Get-NetworkSwitchVlan -CimSession $SessionNew-NetworkSwitchVlan -Name "Contoso07VLAN" -Caption "VLAN 07" -CimSession $SessionOutput
Caption Description Name InstanceID VlanID PSComputerName------- ----------- ----------- ---------- ------ -------------- Vlan_description default Contoso:NetworkVL... 1 10.19.246.18 Vlan_description VLAN0002 Contoso:NetworkVL... 2 10.19.246.18 Caption Description ElementName InstanceID VlanID PSComputerName------- ----------- ----------- ---------- ------ --------------VLAN 07 VLAN0004 Contoso:NetworkVL... 4 10.19.26.4PowerShell
Disable-NetworkSwitchVlan -CimSession $Session -VlanId 74Enable-NetworkSwitchVlan -CimSession $Session -Name "VLAN22"Remove-NetworkSwitchVlan -CimSession $Session -Name "*Contoso*"$VlanName = "Contoso VLAN 07"$Description = "This VLAN is used to create a virtual Contoso company LAN"Set-NetworkSwitchVlanProperty -VlanId 12 -Property @{ElementName = $VlanName; Description = $Description} -CimSession $Session264.5 Port mode, port IP and port properties
| Mode | Option | Meaning | Typical use |
|---|---|---|---|
| Access | -AccessMode -VlanID 1 | One VLAN only | PC, printer |
| Trunk | -TrunkMode -VlanIDs 2,17,22 | Many VLANs on one link | Switch to switch |
| Route | -RouteMode -IpAddress -SubnetAddress | Layer 3 port with its own IP | Routing between networks |
PowerShell
Set-NetworkSwitchPortMode -CimSession $Session -TrunkMode -VlanIDs 2,17,22Set-NetworkSwitchPortMode -CimSession $Session -IpAddress "10.14.1.1" -RouteMode -SubnetAddress "255.255.0.0"Set-NetworkSwitchEthernetPortIPAddress -IpAddress "10.4.10.12" -PortNumber 21 -SubnetAddress "255.255.255.248" -CimSession $SessionRemove-NetworkSwitchEthernetPortIPAddress -CimSession $Session -PortNumber 21- Set-NetworkSwitchPortProperty sets properties like Speed and Description with a hash table.
264.6 Features, save and restore
- Get-NetworkSwitchFeature lists features like SSH, BGP, LACP, LLDP with IsEnabled; -Enabled / -Disabled filter.
- Enable- / Disable-NetworkSwitchFeature by -Name, -InstanceId or -FeatureName.
- Get-NetworkSwitchGlobalData returns the switch's global data (only -CimSession).
- Save-NetworkSwitchConfiguration makes the current config the startup config so changes survive a reboot.
- Restore-NetworkSwitchConfiguration goes back to the last saved startup config.
PowerShell
Disable-NetworkSwitchFeature -CimSession $Session -Name "SSH"Enable-NetworkSwitchFeature -CimSession $Session -InstanceID "Contoso:Feature:2"Get-NetworkSwitchFeature -CimSession $Session -FeatureName "*BGP*"Get-NetworkSwitchGlobalData -CimSession $SessionSave-NetworkSwitchConfiguration -CimSession $SessionRestore-NetworkSwitchConfiguration -CimSession $Session264.7 Lab: a Linux bridge as a managed switch
- Bridge switch1 = the switch; each veth pair = one switch port; pc1/pc2/pc3 = network namespaces (.11, .12, .13 in 192.168.64.0/24).
- bridge, stp and llc were already loaded and veth is built in, so no new kernel module was loaded.
Terminal
ip link add switch1 type bridgeip link set switch1 upip link add pc1-port type veth peer name eth0 netns 51ip link set pc1-port master switch1ip link set pc1-port up# inside pc1:ip addr add 192.168.64.11/24 dev eth0ip link set eth0 up- Disable port idea: ip link set pc2-port down -> state disabled, pc2's eth0 DOWN / NO-CARRIER, ping 100% loss.
- Enable port idea: ip link set pc2-port up -> forwarding again, 3/3 replies; nothing changed inside the PC.
Terminal
ip link set pc2-port downbridge link show dev pc2-portip -br link show eth0 # pc2ping -c 3 -W 1 192.168.64.12 # pc1ip link set pc2-port upOutput
[switchlab]$ bridge link show dev pc2-port5: pc2-port@eth0: <BROADCAST,MULTICAST> mtu 1500 master switch1 state disabled priority 32 cost 2[pc2]$ ip -br link show eth0eth0@if5 DOWN 0e:66:ce:8c:62:40 <NO-CARRIER,BROADCAST,MULTICAST,UP>[pc1]$ ping -c 3 -W 1 192.168.64.12--- 192.168.64.12 ping statistics ---3 packets transmitted, 0 received, 100% packet loss, time 2032ms- VLAN idea: vlan_filtering 1 (module list unchanged), pc1/pc2 access VLAN 10, pc3 VLAN 20 -> pc1 reaches pc2, 100% loss to pc3; move pc3 to VLAN 10 -> 3 replies.
- Feature idea: STP on/off with stp_state 1/0; ip -d link show switch1 is like Global Data.
Terminal
ip link set switch1 type bridge vlan_filtering 1bridge vlan add dev pc1-port vid 10 pvid untaggedbridge vlan del dev pc1-port vid 1# pc2-port: VLAN 10, pc3-port: VLAN 20bridge vlan showping -c 3 -W 1 192.168.64.13 # pc1bridge vlan del dev pc3-port vid 20bridge vlan add dev pc3-port vid 10 pvid untaggedOutput
[switchlab]$ bridge vlan showport vlan-idswitch1 1 PVID Egress Untaggedpc1-port 10 PVID Egress Untaggedpc2-port 10 PVID Egress Untaggedpc3-port 20 PVID Egress Untagged[pc1]$ ping -c 3 -W 1 192.168.64.13--- 192.168.64.13 ping statistics ---3 packets transmitted, 0 received, 100% packet loss, time 2055ms264.8 Common mistakes
- Forgetting Save-NetworkSwitchConfiguration: all port and VLAN changes are lost after a reboot.
- Assuming the module works on any switch: it needs an OMI/CIM agent with the DMTF profile and a CimSession.
- Using a wildcard in Remove-NetworkSwitchVlan -Name without -WhatIf.
- Expecting two hosts in the same subnet but different VLANs to talk.
264.9 Interview tips
- Access port = one VLAN (PC/printer), trunk port = many VLANs on one link (switch to switch), route port = L3 port with IP + mask.
- Changes lost after reboot? The answer is Save-NetworkSwitchConfiguration (startup config).
- NetworkSwitchManager talks to the switch's OMI agent via New-CimSession over WS-Man.
- A VLAN splits one physical switch into separate broadcast domains.