Skip to content

Remove-NetworkAdapter

VMware: Removes a virtual network adapter

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core
[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$VIServer,
    [Parameter(Mandatory = $true)]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true)]
    [string]$VMName,
    [string]$AdapterName
)
Process {
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $vm = Get-VM -Server $vmServer -Name $VMName -ErrorAction Stop
        if ([System.String]::IsNullOrWhiteSpace($AdapterName)) { $adapter = Get-NetworkAdapter -Server $vmServer -VM $vm -ErrorAction Stop }
        else { $adapter = Get-NetworkAdapter -Server $vmServer -VM $vm -Name $AdapterName -ErrorAction Stop }
        if ($null -eq $adapter) { throw "No network adapter found" }
        $null = Remove-NetworkAdapter -NetworkAdapter $adapter -Confirm:$false -ErrorAction Stop
        [PSCustomObject]@{ Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"; Status = "Success"; Message = "Network adapter from VM $VMName successfully removed" }
    }
    catch { throw }
    finally { if ($null -ne $vmServer) { Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue } }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

Virtual machine from which to remove the network adapter

Name of the network adapter to remove; removes all if empty

An interactive directory of PowerShell scripts.