Add DNS Servers to Azure VNET Using PowerShell

In this blog post, we will add DNS servers to an existing Azure Virtual Network (VNET) using PowerShell.

In the previous posts on the topic, we showed how to create a new VNET with subnets using the Az PowerShell module and how to add subnets to an existing VNET using PowerShell.

Today we are going to take an existing VNET with subnets and add DNS servers to it.

The process to add DNS Servers is the same as adding subnets to an existing VNET, but we DNS we add servers, and we still need to create the VNET object in memory before applying the changes.


In the code below, I am recreating in memory my VNET with all the subnets (Apps, Servers, DMZ), and in line number four, I’m creating the VNET in memory and adding the DNS servers to it (, )

$appssubnet = New-AzVirtualNetworkSubnetConfig -Name servers -AddressPrefix ""
$serversubnet = New-AzVirtualNetworkSubnetConfig -Name apps -AddressPrefix ""
$dmz = New-AzVirtualNetworkSubnetConfig -Name dmz -AddressPrefix ""
$updatedvnet = New-AzVirtualNetwork -Name MyVNET -ResourceGroupName vnetlab -Location australiasoutheast -AddressPrefix "" -Subnet $serversubnet, $dmz, $appssubnet -DnsServer, -Force:$true
$updatedvnet | Set-AzVirtualNetwork

The last line will set the VNET in Azure modify the existing VNET.