• Post category:Azure
  • Post comments:0 Comments
  • Reading time:3 mins read

Jeff Brown

Cloud and DevOps Engineer specializing in Microsoft 365, Azure, and PowerShell. Twitter | LinkedIn

When creating a virtual network in the Azure Portal, there is a requirement to create the first subnet in the virtual network. However, what do you do if you don’t want to create the first subnet right away or if you want to create more than one to start? Azure PowerShell to the rescue! Using the PowerShell cmdlets, this post will show you how to create a virtual with no subnets or with multiple subnets to start.

First, if you don’t have the Az PowerShell module installed, use the command Install-Module -Name Az to install it locally on your system (more information found here at the PowerShell Gallery site) or just connect using the Azure Cloud Shell at https://shell.azure.com. Next, connect to your Azure tenant using the Connect-AzAccount command where you are prompted for a username and password:

Next, I’ll create a resource group using the New-AzResourceGroup command and save it to a variable called $rg so I can reference it later.

Using the New-AzVirtualNetwork command, I can specify the name of my new virtual network, resource group name (using the $rg variable), the location of the virtual network (also referencing the same location as my resource group) and the address prefix. Afterwards, I print the new virtual network information to the screen, notice I do not have any subnets configured.

Now when I’m ready, I can create a subnet for the virtual network using the Add-AzVirtualNetworkSubnetConfig command, specifying the name of the subnet, the prefix for the subnet, then the virtual network it will be associated with using my $vnetA variable.

To associate the subnet to the virtual network, pipe the $subnet1 variable into the Set-AzVirtualNetwork command. This will use the properties in the variable to configure the virtual network with the new subnet.

But now let’s go the other direction. What if I want to create a virtual network and create it with multiple subnets at the same time? To do this, I use the New-AzVirtualNetworkSubnetConfig to create subnets ahead of time. I will create three subnets here using these commands and save them to their own variables:

$subnetB1 = New-AzVirtualNetworkSubnetConfig -Name subnet1 -AddressPrefix

$subnetB2 = New-AzVirtualNetworkSubnetConfig -Name subnet2 -AddressPrefix

$subnetB3 = New-AzVirtualNetworkSubnetConfig -Name subnet3 -AddressPrefix

Output of a saved subnet:

Now when creating a second virtual network, I can specify creating multiple subnets using the -Subnet parameter and the saved subnet information from my variables:

We can verify the subnet configuration by looking at the properties of our saved $vnetB virtual network:

And also by verifying the virtual network properties in the portal:

Questions or comments? If so, drop me a note below or find me on Twitter or LinkedIn to discuss further.

Leave a Reply