Skip to content

Get-HyperVVirtualSwitch

Hyper-V: Lists all virtual switches on a Hyper-V host

#Requires -Version 5.1
#Requires -Modules Hyper-V

[CmdletBinding()]
Param (
    [string]$ComputerName = "localhost",

    [PSCredential]$Credential,

    [ValidateSet('External', 'Internal', 'Private')]
    [string]$SwitchType
)

Process {
    try {
        $params = @{
            'ComputerName' = $ComputerName
            'ErrorAction'  = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }
        if ($SwitchType) { $params.Add('SwitchType', $SwitchType) }

        $switches = Get-VMSwitch @params
        
        $results = foreach ($s in $switches) {
            [PSCustomObject]@{
                Name               = $s.Name
                SwitchType         = $s.SwitchType
                AllowManagementOS  = $s.AllowManagementOS
                InterfaceDescription = $s.NetAdapterInterfaceDescription
                ComputerName       = $s.ComputerName
                Status             = "Available" # Basic status indicator
            }
        }

        Write-Output ($results | Sort-Object Name)
    }
    catch {
        throw
    }
}

Specifies the name of the Hyper-V host. Defaults to the local machine.

Specifies the credentials to use for the remote connection.

Optional. Specifies the type of virtual switches to retrieve (External, Internal, Private).

An interactive directory of PowerShell scripts.