Configure routing preference for a public IP address using Azure PowerShell

This article shows you how to configure routing preference via ISP network (Internet option) for a public IP address using Azure PowerShell. After creating the public IP address, you can associate it with the following Azure resources for inbound and outbound traffic to the internet:

  • Virtual machine
  • Virtual machine scale set
  • Azure Kubernetes Service (AKS)
  • Internet-facing load balancer
  • Application Gateway
  • Azure Firewall

By default, the routing preference for public IP address is set to the Microsoft global network for all Azure services and can be associated with any Azure service.

If you don't have an Azure subscription, create a free account now.

Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code or command block. Selecting Try It doesn't automatically copy the code or command to Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Button to launch Azure Cloud Shell.
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Screenshot that shows the Cloud Shell button in the Azure portal

To use Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block (or command block) to copy the code or command.

  3. Paste the code or command into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code or command.

If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 6.9.0 or later. Run Get-Module -ListAvailable Az to find the installed version. If you need to upgrade, see Install Azure PowerShell module. If you are running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.

Create a resource group

Create a resource group with New-AzResourceGroup. This example creates a resource group named myResourceGroup in the eastus location:

$rg = New-AzResourceGroup -Name myResourceGroup -Location EastUS

Create a Public IP with Internet routing preference

The following command creates a new public IP with a routing preference type as Internet in the East US Azure region:

$iptagtype="RoutingPreference"
$tagName = "Internet"
$ipTag = New-AzPublicIpTag -IpTagType $iptagtype -Tag $tagName 
# attach the tag
$publicIp = New-AzPublicIpAddress  `
-Name "MyPublicIP" `
-ResourceGroupName $rg.ResourceGroupName `
-Location $rg.Location `
-IpTag $ipTag `
-AllocationMethod Static `
-Sku Standard `
-IpAddressVersion IPv4

You can associate the above created public IP address with a Windows or Linux virtual machine. Use the CLI section on the tutorial page: Associate a public IP address to a virtual machine to associate the Public IP to your VM. You can also associate the public IP address created above with an Azure Load Balancer, by assigning it to the load balancer frontend configuration. The public IP address serves as a load-balanced virtual IP address (VIP).

Clean up resources

If no longer needed, you can use the Remove-AzResourceGroup command to remove the resource group, VM, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup

Next steps