You dont have javascript enabled! Please enable it!

How To Rename The NIC Interface for an Azure Virtual Machine

5 Min. Read

Updated – 21/04/2022 – Based on the user’s feedback, the tool below was completely updated to support multiple network interfaces. You can now rename multiple NICs attached to an Azure virtual machine.

How do I rename my Azure Nic?

Introduction

Renaming the NIC interface for an Azure VM is not a new problem. If you find yourself having to do this, you know how painful it is. This is the case when you deploy a Virtual Machine using the Azure Portal, however, if you deploy the VM using Azure PowerShell, ARM template, Bicep, or Azure CLI, then you can specify the NIC interface name at deployment time.

Once the VM is deployed using the portal, Azure will end up creating a random name for the Network interface as shown in the following screenshot:

How To Rename The NIC Interface for an Azure Virtual Machine 1

If you are following a standard naming convention in your company, then you want to rename the NIC interface by following the steps below as documented by Microsoft:

  1. Stop (de-allocate) the VM.
  2. Dissociate the public IP address from the NIC interface (if exists).
  3. Create a new network adapter interface.
  4. Assign the new NIC interface to the VM.
  5. Remove the old NIC interface from the VM.
  6. Preserve the old NIC settings and apply them to the VM (Private IP address, Network Security Group, Public IP, etc.).
  7. Delete the old NIC interface resource.
  8. Start the VM.

In this article, we will share with you how to automate and rename the NIC interface for an Azure VM with PowerShell. The same can apply whether you are using a Linux or Windows VM.

If you want to rename the OS Disk for an Azure virtual machine, please check the following article.

If you want to rename an Azure virtual machine, please check the following article.

If you want to rename the Data Disks for an Azure virtual machine, please check the following article.

Rename Azure VM NIC Interface

You have a couple of options to run the script, you can either use Azure Cloud Shell, Visual Studio Code, or the new Windows Terminal.

The Script works with PowerShell 5.1 or PowerShell 7.2.x (core) with the Az module.

You can use the following PowerShell command to install and update the “Az module”.

# Make sure you have the latest version of PowerShellGet installed
Install-Module -Name PowerShellGet -Force

# Install and update to the latest Az PowerShell module
Install-Module -Name Az -AllowClobber -Force

.EXAMPLE 1

.\Rename-AzVMNIC.ps1 -resourceGroup [ResourceGroupName] -VMName [VMName] -Verbose

This example will rename the NIC interface for the specified VM, you need to specify the Resource Group name, and the VM name only.

The script will prompt you to enter the NIC interface name, please note that the network interface card name must be unique in the same resource group.

The script will preserve the old network settings and apply them to the new network interface including the public IP address if exists.

.EXAMPLE 2

.\Rename-AzVMNIC.ps1 -resourceGroup [ResourceGroupName] -VMName [VMName] -NewNicName [NewNicName] -Verbose

This example will rename the NIC interface for the specified VM, you need to specify the Resource Group name, the VM name, and the New Nic Name that you want.

The script will preserve the old network settings and apply them to the new network interface including the public IP address if exists.

Please note that the VM will shut down during this operation. You need to plan a small maintenance window if it’s an important VM. The entire process will take around 5 minutes.

Note: The script has not been tested when the VM is a member of an Availability Set. Please share your feedback in the comment section below!

Here is an example of the output once you run this tool:

How To Rename The NIC Interface for an Azure Virtual Machine 2

PowerShell Tool

The complete script is detailed below to automate the entire process:

<#
.SYNOPSIS
Rename Azure VM Network Interfaces.

.DESCRIPTION
Rename single or multiple Network Adapter interfaces attached to an Azure Virtual Machine (Linux and Windows).

.NOTES
File Name : Rename-AzVMNIC.ps1
Author    : Microsoft MVP/MCT - Charbel Nemnom
Version   : 1.4
Date      : 22-September-2019
Update    : 30-June-2022
Requires  : PowerShell 5.1 or PowerShell 7.1.x (Core)
Module    : Az Module
OS        : Windows or Linux VMs

.LINK
To provide feedback or for further assistance please visit: https://charbelnemnom.com

.EXAMPLE
.\Rename-AzVMNIC.ps1 -resourceGroup [ResourceGroupName] -VMName [VMName] -NewNicName [NewNicName] -Verbose
This example will rename the NIC interface for the specified VM, you need to specify the Resource Group name, VM name, and the new NIC name.
The script will preserve the old network settings and apply them to the new network interface.
#>

[CmdletBinding()]
Param (
    [Parameter(Position = 0, Mandatory = $true, HelpMessage = 'Enter the Resource Group of the VM')]
    [Alias('rg')]
    [String]$resourceGroup,

    [Parameter(Position = 1, Mandatory = $True, HelpMessage = 'Enter Azure VM name')]
    [Alias('VM')]
    [String]$VMName,

    [Parameter(Position = 2, HelpMessage = 'Enter New NIC name')]
    [Alias('NIC')]
    [String]$NewNicName     
)

#! Check Azure Connection
Try {
    Write-Verbose "Connecting to Azure Cloud..."
    Connect-AzAccount -ErrorAction Stop | Out-Null
}
Catch {
    Write-Warning "Cannot connect to Azure Cloud. Please check your credentials. Exiting!"
    Break
}

#! Get the details of the VM
Write-Verbose "Get the VM information details: $VMName"
$VM = Get-AzVM -Name $VMName -ResourceGroupName $resourceGroup

#! Get all the virtual NIC interfaces, names and details
Write-Verbose "Get all the virtual NIC interface names and details..."
$oldNics = $VM.NetworkProfile.NetworkInterfaces
for ($i = 0; $i -lt $oldNics.Count; $i++) {
    $PIpName = $null
    $oldNicName = $oldNics[$i].Id.Split('/')[-1]
    $vNic = Get-AzNetworkInterface -Name $oldNicName -ResourceGroupName $resourceGroup

    $NewNicName = Read-Host "`nEnter the NIC interface name - Network interface card name must be unique"

    #! Get the public ip address of the virtual machine if exists.
    If ($vNic.IpConfigurations.publicIPAddress.Id -ne $null) {
        $PIpName = $VNic.IpConfigurations.publicIPAddress.Id.Split('/')[-1]
        $vNic.IpConfigurations.publicipaddress.id = $null
        Write-Verbose "Dissociate the public IP address from the VM: $PIpName"
        Set-AzNetworkInterface -NetworkInterface $vnic | Out-Null
    }

    #! Stop the Azure VM
    if ( (Get-AzVM -Name $VMName -status).PowerState -eq "VM running" ) {
        Write-Verbose "Stop and deallocate the VM: $VMName, please wait..."
        Stop-AzVM -Name $VMName -ResourceGroupName $resourceGroup -Force -Confirm:$false | Out-Null
    }

    #! Create the new virtual NIC interface
    Write-Verbose "Creating the new virtual Network interface..."
    $NIC = New-AzNetworkInterface -Name $NewNicName -ResourceGroupName $resourceGroup `
        -Location $VM.Location -SubnetId $vnic.IpConfigurations.Subnet.Id `
        -IpConfigurationName $vnic.IpConfigurations.Name

    #! Remove the old NIC interface from the VM
    Write-Warning "Removing the old NIC interface: $($oldNicName) from the VM: $VMName"
    Remove-AzVMNetworkInterface -vm $VM -NetworkInterfaceIDs $vNic.Id | Out-Null

    #! Add the new NIC interface
    Write-Verbose "Adding the new network adapter interface to the VM..."
    Add-AzVMNetworkInterface -VM $VM -Id $NIC.Id -Primary | Update-AzVM -ResourceGroupName $resourceGroup | Out-Null

    #! Delete the old NIC interface resource
    Write-Warning "Deleting the old NIC interface: $($oldNicName)"
    Remove-AzNetworkInterface -Name $oldNicName -ResourceGroupName $vNic.ResourceGroupName -Force -Confirm:$false

    #! Update the new virtual NIC settings
    $NIC = Get-AzNetworkInterface -Name $NewNicName -ResourceGroupName $resourceGroup
    If ($vNic.Tag -ne $null) {
        $NIC.Tag = $vNIC.Tag
    }
    $NIC.DnsSettings = $vNIC.DnsSettings
    $NIC.EnableIPForwarding = $vNIC.EnableIPForwarding
    $NIC.EnableAcceleratedNetworking = $vNIC.EnableAcceleratedNetworking
    $NIC.NetworkSecurityGroup = $vNIC.NetworkSecurityGroup

    #! Set the new virtual NIC settings
    Write-Verbose "Set the new NIC interface settings..."
    If ($NIC.IpConfigurations.PrivateIpAddress -ne $vNIC.IpConfigurations.PrivateIpAddress) {
        Set-AzNetworkInterfaceIpConfig -NetworkInterface $NIC -Name $NIC.IpConfigurations.Name `
            -PrivateIpAddressVersion $vNIC.IpConfigurations.PrivateIpAddressVersion `
            -PrivateIpAddress $vNIC.IpConfigurations.PrivateIpAddress -SubnetId $vnic.IpConfigurations.Subnet.id | Out-Null
    }
    Set-AzNetworkInterface -NetworkInterface $NIC | Out-Null

    #! Associate a public IP for the VM
    If ($PIpName) {
        $PublicIp = Get-AzPublicIpAddress -Name $PIpName -ResourceGroupName $resourceGroup
        $NIC | Set-AzNetworkInterfaceIpConfig -Name $NIC.IpConfigurations.Name -PublicIPAddress $PublicIp `
            -Subnet $vnic.IpConfigurations.Subnet | Out-Null
        Write-Verbose "Associate the public IP address to the VM: $PIpName"
        Set-AzNetworkInterface -NetworkInterface $NIC | Out-Null
    }
}

#! Start the VMName
Write-Verbose "Start the VM: $VMName, please wait..."
Start-AzVM -Name $VMName -ResourceGroupName $resourceGroup -Confirm:$false | Out-Null

If you look back in the Azure Portal, we can see that the new NIC interface name is attached successfully and the previous settings are preserved.

How To Rename The NIC Interface for an Azure Virtual Machine 3

Summary

In this article, we showed you how to rename and change the NIC on Azure VM using PowerShell.

If you have any feedback or changes that everyone should receive, please feel free to leave a comment below.

That’s it there you have it. Happy Azure Networking!

__
Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

Related Posts

Previous

Update Rollup 8 for System Center 2016 is Now Available #SystemCenter #SysCtr

Passed MS-500 Exam – Microsoft 365 Certified: Security Administrator Associate

Next

5 thoughts on “How To Rename The NIC Interface for an Azure Virtual Machine”

Leave a comment...

  1. Hello Devan, thanks for your comment! Please note that you should be able to copy now from the ‘Black Code Block’. Please try again and let me know if it works for you. Thanks!

  2. Hi,
    If I run the script with -NewNicName parameter but it gives me the error ” A parameter cannot be found that matches parameter name ‘NewNICName’.”
    I left it away and the script prompts me for the new nic name which worked well.
    Regards,
    Michael

  3. Hello Michael, thank you for the feedback and comment!
    Yes, I missed adding the “NewNICName” parameter.
    The script was updated so you can pass the -NewNicName parameter now.
    Thanks!

Let me know what you think, or ask a question...

error: Alert: The content of this website is copyrighted from being plagiarized! You can copy from the 'Code Blocks' in 'Black' by selecting the Code. Thank You!