How to Rename an Azure Virtual Network Using PowerShell


8 views

Azure doesn't provide a direct rename operation for virtual networks (VNets) through the portal UI or PowerShell cmdlets. This limitation exists because VNet names are embedded in resource URIs and DNS records across Azure services.

The most efficient method involves creating a new VNet with the desired name and migrating resources. Here's the PowerShell implementation:


# Step 1: Export existing VNet configuration
$oldVNet = Get-AzVirtualNetwork -Name "OldVNetName" -ResourceGroupName "YourRG"
$oldVNet | Export-Clixml -Path "OldVNetConfig.xml"

# Step 2: Create new VNet with modified properties
$newVNet = New-AzVirtualNetwork 
    -Name "NewVNetName" 
    -ResourceGroupName $oldVNet.ResourceGroupName 
    -Location $oldVNet.Location 
    -AddressPrefix $oldVNet.AddressSpace.AddressPrefixes

# Step 3: Recreate subnets (example for one subnet)
$subnetConfig = Add-AzVirtualNetworkSubnetConfig 
    -Name "default" 
    -AddressPrefix "10.0.0.0/24" 
    -VirtualNetwork $newVNet
$newVNet | Set-AzVirtualNetwork
  • Update Network Security Groups (NSGs) references
  • Modify route tables associated with the old VNet
  • Reconfigure VPN/ExpressRoute connections
  • Update application configurations pointing to old VNet

For complex environments, consider this script to move NICs between VNets:


$vms = Get-AzVM -ResourceGroupName "YourRG"
foreach ($vm in $vms) {
    $nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces.Id
    $nic.IpConfigurations[0].Subnet.Id = $nic.IpConfigurations[0].Subnet.Id.Replace(
        "OldVNetName", 
        "NewVNetName"
    )
    $nic | Set-AzNetworkInterface
}

Export the existing VNet as ARM template, modify the JSON, and redeploy:


Export-AzResourceGroup -ResourceGroupName "YourRG" 
    -Resource "Microsoft.Network/virtualNetworks/OldVNetName" 
    -Path "C:\temp\vnet_template.json"

# Edit the JSON file to change name and redeploy
New-AzResourceGroupDeployment -ResourceGroupName "YourRG" 
    -TemplateFile "C:\temp\vnet_template.json"

Many Azure administrators reach a point where they need to rename existing virtual networks, only to discover that Microsoft doesn't provide a direct rename operation. The portal UI lacks this functionality, and the Azure REST API similarly doesn't expose a rename endpoint. This limitation persists even in large-scale deployments where rebranding or organizational changes necessitate network name updates.

Azure Resource Manager treats virtual network names as immutable properties once created. The name becomes part of the resource's unique identifier and is referenced throughout dependent resources. Here's why direct renaming isn't supported:

  • Network name is embedded in DNS records
  • Name is hardcoded in NIC configurations
  • Route tables and NSGs reference the original name
  • Peering connections would break

While you can't rename an existing VNet, you can migrate resources to a new network with your desired name. Here's the PowerShell approach:


# Step 1: Create new VNet with desired name
$newVNetParams = @{
    Name              = "New-Prod-VNet"
    ResourceGroupName = "Your-RG"
    Location          = "eastus"
    AddressPrefix     = "10.0.0.0/16"
}
$newVnet = New-AzVirtualNetwork @newVNetParams

# Step 2: Recreate subnets in new VNet
$subnetConfig = Add-AzVirtualNetworkSubnetConfig 
    -Name "default" 
    -AddressPrefix "10.0.1.0/24" 
    -VirtualNetwork $newVnet
$newVnet | Set-AzVirtualNetwork

# Step 3: Migrate resources (example for VMs)
$vms = Get-AzVM -ResourceGroupName "Your-RG"
foreach ($vm in $vms) {
    $nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id
    $nic.IpConfigurations[0].Subnet = $subnetConfig
    $nic | Set-AzNetworkInterface
}

When executing this migration:

  • Schedule during maintenance windows - there will be network downtime
  • Update all automation scripts referencing the old VNet name
  • Review Network Security Groups and route tables
  • Check for service endpoints and private links
  • Update any VPN or ExpressRoute configurations

For complex environments, consider using ARM templates to redeploy your infrastructure with the new naming convention:


{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "resources": [
        {
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2023-05-01",
            "name": "New-Prod-VNet",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "10.0.0.0/16"
                    ]
                },
                "subnets": [
                    {
                        "name": "default",
                        "properties": {
                            "addressPrefix": "10.0.1.0/24"
                        }
                    }
                ]
            }
        }
    ]
}