Skip to content

Set-HyperVMacAddressRange

Hyper-V: Configures the dynamic MAC address range for a Hyper-V host

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

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

    [PSCredential]$Credential,

    [Parameter(Mandatory = $true)]
    [ValidatePattern('^[0-9A-Fa-f]{12}$')]
    [string]$Minimum,

    [Parameter(Mandatory = $true)]
    [ValidatePattern('^[0-9A-Fa-f]{12}$')]
    [string]$Maximum
)

Process {
    try {
        $params = @{
            'ComputerName'      = $ComputerName
            'MacAddressMinimum' = $Minimum
            'MacAddressMaximum' = $Maximum
            'Confirm'           = $false
            'ErrorAction'       = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }

        Set-VMHost @params

        $hostInfo = Get-VMHost -ComputerName $ComputerName -ErrorAction Stop
        
        $result = [PSCustomObject]@{
            ComputerName      = $hostInfo.Name
            MacAddressMinimum = $hostInfo.MacAddressMinimum
            MacAddressMaximum = $hostInfo.MacAddressMaximum
            Action            = "MacAddressRangeUpdated"
            Status            = "Success"
            Timestamp         = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    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 minimum MAC address in hexadecimal format (e.g., 00155D000000).

Specifies the maximum MAC address in hexadecimal format (e.g., 00155DFFFFFF).

An interactive directory of PowerShell scripts.