Skip to content

Get-HyperVNetworkAdapterInfo

Hyper-V: Retrieves properties of a specific Hyper-V network adapter

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

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

    [PSCredential]$Credential,

    [Parameter(Mandatory = $true)]
    [string]$Name,

    [switch]$ManagementOS,

    [switch]$IncludeVlan
)

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

        $adapter = Get-VMNetworkAdapter @params
        
        $results = foreach ($a in $adapter) {
            $obj = [PSCustomObject]@{
                Name           = $a.Name
                SwitchName     = $a.SwitchName
                MacAddress     = $a.MacAddress
                Status         = $a.Status
                IsManagementOs = $a.IsManagementOs
                IsExternal     = $a.IsExternalAdapter
                ComputerName   = $a.ComputerName
            }

            if ($IncludeVlan) {
                $vlan = Get-VMNetworkAdapterVlan -VMNetworkAdapter $a -ErrorAction SilentlyContinue
                if ($vlan) {
                    $obj | Add-Member -MemberType NoteProperty -Name "VlanMode" -Value $vlan.OperationMode
                    $obj | Add-Member -MemberType NoteProperty -Name "VlanId" -Value $vlan.AccessVlanId
                }
            }
            $obj
        }

        Write-Output $results
    }
    catch {
        throw
    }
}

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

Specifies the credentials to use for the remote connection.

Specifies the name of the network adapter to retrieve.

Off

If set, retrieves adapters from the management OS instead of virtual machines.

Off

If set, includes VLAN configuration properties in the output.

An interactive directory of PowerShell scripts.