Create and manage route tables using Azure PowerShell

A route table contains a set of rules, called routes, that determine where network traffic from your subnet or gateway is directed.

You can create a route table and associate it with one or more subnets. When traffic from a subnet is directed to a network, the route table associated with the subnet is checked. If a matching route is found, the traffic is directed according to the route.

Note This article explains how to manage route tables using Azure PowerShell. You can also manage route tables using the Azure portal and Azure CLI.

Prerequisites

  • Install and configure Azure PowerShell.
  • Connect to your Azure account using Connect-AzAccount.

Create a route table

To create a route table, use the New-AzRouteTable cmdlet. The following example creates a route table named MyRouteTable in the MyResourceGroup resource group:

New-AzRouteTable -Name "MyRouteTable" -ResourceGroupName "MyResourceGroup" -Location "East US"

To view the properties of the created route table, use the Get-AzRouteTable cmdlet:

Get-AzRouteTable -Name "MyRouteTable" -ResourceGroupName "MyResourceGroup"

Add routes to a route table

To add routes to a route table, use the Add-AzRouteConfig cmdlet. The following example adds a route with an address prefix of 10.0.0.0/16, a next hop type of VirtualAppliance, and a next hop IP address of 10.0.0.4:

$routeTable = Get-AzRouteTable -Name "MyRouteTable" -ResourceGroupName "MyResourceGroup"
Add-AzRouteConfig -Name "RouteToAppliance" -RouteTable $routeTable -AddressPrefix "10.0.0.0/16" -NextHopType VirtualAppliance -NextHopIpAddress "10.0.0.4"
Set-AzRouteTable -RouteTable $routeTable

To add a default route that directs all traffic to the internet:

$routeTable = Get-AzRouteTable -Name "MyRouteTable" -ResourceGroupName "MyResourceGroup"
Add-AzRouteConfig -Name "DefaultRouteToInternet" -RouteTable $routeTable -AddressPrefix "0.0.0.0/0" -NextHopType Internet
Set-AzRouteTable -RouteTable $routeTable

To list all routes in a route table:

Get-AzRouteTable -Name "MyRouteTable" -ResourceGroupName "MyResourceGroup" | Select-Object -ExpandProperty Routes

Associate a route table with a subnet

To associate a route table with a subnet, use the Set-AzVirtualNetworkSubnetConfig cmdlet. The following example associates the MyRouteTable route table with the subnet named MySubnet in the virtual network MyVnet:

$vnet = Get-AzVirtualNetwork -Name "MyVnet" -ResourceGroupName "MyResourceGroup"
$subnet = Get-AzVirtualNetworkSubnetConfig -Name "MySubnet" -VirtualNetwork $vnet
$routeTable = Get-AzRouteTable -Name "MyRouteTable" -ResourceGroupName "MyResourceGroup"
Set-AzVirtualNetworkSubnetConfig -Name "MySubnet" -VirtualNetwork $vnet -AddressPrefix "10.0.1.0/24" -RouteTable $routeTable
Set-AzVirtualNetwork -VirtualNetwork $vnet

To remove a route table association from a subnet, set the RouteTable parameter to $null:

$vnet = Get-AzVirtualNetwork -Name "MyVnet" -ResourceGroupName "MyResourceGroup"
$subnet = Get-AzVirtualNetworkSubnetConfig -Name "MySubnet" -VirtualNetwork $vnet
Set-AzVirtualNetworkSubnetConfig -Name "MySubnet" -VirtualNetwork $vnet -AddressPrefix "10.0.1.0/24" -RouteTable $null
Set-AzVirtualNetwork -VirtualNetwork $vnet

Remove a route from a route table

To remove a route from a route table, use the Remove-AzRouteConfig cmdlet:

$routeTable = Get-AzRouteTable -Name "MyRouteTable" -ResourceGroupName "MyResourceGroup"
Remove-AzRouteConfig -Name "RouteToAppliance" -RouteTable $routeTable
Set-AzRouteTable -RouteTable $routeTable

Delete a route table

To delete a route table, use the Remove-AzRouteTable cmdlet:

Remove-AzRouteTable -Name "MyRouteTable" -ResourceGroupName "MyResourceGroup"