Create a virtual network using PowerShell

An Azure virtual network (VNet) is a representation of your own network in the cloud. You can control your Azure network settings and define DHCP address blocks, DNS settings, security policies, and routing. You can also further segment your VNet into subnets and deploy Azure IaaS virtual machines (VMs) and PaaS role instances, in the same way you can deploy physical and virtual machines to your on-premises datacenter. In essence, you can expand your network to Azure, bringing your own IP address blocks. Read the virtual network overview if you are not familiar with VNets.

Azure has two deployment models: Azure Resource Manager and classic. Microsoft recommends creating resources through the Resource Manager deployment model. To learn more about the differences between the two models, read the Understand Azure deployment models article.

This article explains how to create a VNet through the Resource Manager deployment model using PowerShell. You can also create a VNet through Resource Manager using other tools or create a VNet through the classic deployment model by selecting a different option from the following list:

Scenario

To better illustrate how to create a VNet and subnets, this document will use the scenario below.

VNet scenario

In this scenario you will create a VNet named TestVNet with a reserved CIDR block of 192.168.0.0./16. Your VNet will contain the following subnets:

  • FrontEnd, using 192.168.1.0/24 as its CIDR block.
  • BackEnd, using 192.168.2.0/24 as its CIDR block.

Create a virtual network

To create a virtual network using PowerShell, complete the following steps:

  1. Install and configure Azure PowerShell, by following the steps in the How to Install and Configure Azure PowerShell article.

  2. If necessary, create a new resource group, as shown below. For this scenario, create a resource group named TestRG. For more information about resource groups, visit Azure Resource Manager Overview.

    New-AzureRmResourceGroup -Name TestRG -Location centralus
    

    Expected output:

     ResourceGroupName : TestRG
     Location          : centralus
     ProvisioningState : Succeeded
     Tags              :
     ResourceId        : /subscriptions/[Subscription Id]/resourceGroups/TestRG    
    
  3. Create a new VNet named TestVNet:

    New-AzureRmVirtualNetwork -ResourceGroupName TestRG -Name TestVNet `
    -AddressPrefix 192.168.0.0/16 -Location centralus
    

    Expected output:

     Name                  : TestVNet
     ResourceGroupName     : TestRG
     Location              : centralus
     Id                    : /subscriptions/[Subscription Id]/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet
     Etag                   : W/"[Id]"
     ProvisioningState          : Succeeded
     Tags                       : 
     AddressSpace               : {
                                "AddressPrefixes": [
                                  "192.168.0.0/16"
                                ]
                               }
     DhcpOptions                : {}
     Subnets                    : []
     VirtualNetworkPeerings     : []
    
  4. Store the virtual network object in a variable:

    $vnet = Get-AzureRmVirtualNetwork -ResourceGroupName TestRG -Name TestVNet
    
    Tip

    You can combine steps 3 and 4 by running $vnet = New-AzureRmVirtualNetwork -ResourceGroupName TestRG -Name TestVNet -AddressPrefix 192.168.0.0/16 -Location centralus.

  5. Add a subnet to the new VNet variable:

    Add-AzureRmVirtualNetworkSubnetConfig -Name FrontEnd `
    -VirtualNetwork $vnet -AddressPrefix 192.168.1.0/24
    

    Expected output:

     Name                  : TestVNet
     ResourceGroupName     : TestRG
     Location              : centralus
     Id                    : /subscriptions/[Subscription Id]/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet
     Etag                  : W/"[Id]"
     ProvisioningState     : Succeeded
     Tags                  :
     AddressSpace          : {
                               "AddressPrefixes": [
                                 "192.168.0.0/16"
                               ]
                             }
     DhcpOptions           : {}
     Subnets             : [
                               {
                                 "Name": "FrontEnd",
                                 "AddressPrefix": "192.168.1.0/24"
                               }
                             ]
     VirtualNetworkPeerings     : []
    
  6. Repeat step 5 above for each subnet you want to create. The following command creates the BackEnd subnet for the scenario:

    Add-AzureRmVirtualNetworkSubnetConfig -Name BackEnd `
    -VirtualNetwork $vnet -AddressPrefix 192.168.2.0/24
    
  7. Although you create subnets, they currently only exist in the local variable used to retrieve the VNet you create in step 4 above. To save the changes to Azure, run the following command:

    Set-AzureRmVirtualNetwork -VirtualNetwork $vnet
    

    Expected output:

     Name                  : TestVNet
     ResourceGroupName     : TestRG
     Location              : centralus
     Id                    : /subscriptions/[Subscription Id]/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet
     Etag                  : W/"[Id]"
     ProvisioningState     : Succeeded
     Tags                  :
     AddressSpace          : {
                               "AddressPrefixes": [
                                 "192.168.0.0/16"
                               ]
                             }
     DhcpOptions           : {
                               "DnsServers": []
                             }
     Subnets               : [
                               {
                                 "Name": "FrontEnd",
                                 "Etag": "W/\"[Id]\"",
                                 "Id": "/subscriptions/[Subscription Id]/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet/subnets/FrontEnd",
                                 "AddressPrefix": "192.168.1.0/24",
                                 "IpConfigurations": [],
                                 "ProvisioningState": "Succeeded"
                               },
                               {
                                 "Name": "BackEnd",
                                 "Etag": "W/\"[Id]\"",
                                 "Id": "/subscriptions/[Subscription Id]/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet/subnets/BackEnd",
                                 "AddressPrefix": "192.168.2.0/24",
                                 "IpConfigurations": [],
                                 "ProvisioningState": "Succeeded"
                               }
                             ]
     VirtualNetworkPeerings : []
    

Next steps

Learn how to connect: