Hyper-V How To: Clear All Virtual Networking using Script

Some friends here on the Hyper-V team shared a PowerShell 2.0 script for clearing all the VM’s networking objects:

# Clear out all Virtual Networking objects

# WARNING: Do not run this script remotely, as it will interrupt any network
# connections during execution and restore normal, non-virtual networking on completion

$vsms = gwmi -namespace root\virtualization Msvm_VirtualSwitchManagementService
gwmi -namespace root\virtualization Msvm_InternalEthernetPort |`
    foreach{$vsms.DeleteInternalEthernetPort($_)}

gwmi -namespace root\virtualization Msvm_VirtualSwitch |`
    foreach{$vsms.DeleteSwitch($_)}

gwmi -namespace root\virtualization Msvm_ExternalEthernetPort |`
    foreach{$vsms.UnbindExternalEthernetPort($_)}

 

For more info on how to use PS cmdlets see: https://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx

See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!

For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V PowerShell Example Scripts.zip-download