Hyper-V How To: Create a Virtual Switch using Script

Some friends here on the Hyper-V team shared a PowerShell 2.0 script for creating a virtual switch:

# Create a Virtual Switch

    [string]$vsName = $(throw "Must specify virtual switch name")

# Get the Virtual Switch Management Service
$vsms = gwmi -namespace root\virtualization Msvm_VirtualSwitchManagementService

# Use CreateSwitch method to specify the name, friendly name, learnable addresses,
# and authorization scope of the created switch (use "" for the root scope)
$result = $vsms.CreateSwitch($vsName,$vsName,2048,"")
if($result.ReturnValue -ne 0){
    throw "Error: $($result.ReturnValue)"
return ([wmi]$result.CreatedVirtualSwitch)


For more info on how to use PS cmdlets see: http://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%20PowerShell%20Example%20Scripts.zip-download

Comments (0)

Skip to main content