Azure PowerShell – Create Virtual Network Within Resource Group

| |

This is still a work in progress to include all the variables but the script below will create a Resource Group then create a Virtual Network with 3 subnets and a gateway along with custom DNS.

PowerShell
# Main Reference: https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-create-vnet-arm-ps

Clear

Write-verbose "Setting Environment Variables - Start"

$VNetName = azuredev-eastus-vnet
$VNetPrefix = 10.40.0.0/24
$SubNet01Name = AppSubnet
$SubNet01Prefix = 10.40.0.0/25
$SubNet02Name = ServicesSubnet
$SubNet02Prefix = 10.40.0.128/26
$SubNet03Name = DataSubnet
$SubNet03Prefix = 10.40.0.192/27
$GWSubName = Gateway
$GWSubPrefix = 10.40.0.224/29
$VPNClientAddressPool = 172.16.201.0/24
$RG = azuredev-eastus-rg
$Location = East US
$DNS01 = 208.67.222.222
$DNS02 = 208.67.220.220
$GWName = azuredev-eastus-gw
$GWIPName = azuredev-eastus-gwip
$GWIPconfName = azuredev-eastus-gwipconf
$P2SRootCertName = azuredev-eastus-RootCertificate

Write-verbose "Setting Environment Variables - Stop"

# Log into Azure Account
# Login-AzureRmAccount

# List all Azure Subscriptions
# Get-AzureSubscription | Format-Table subscriptionname, subscriptionid

#Write-Verbose "Select Azure Subscription"
#Select-AzureRmSubscription -SubscriptionId <Subscription ID> # Dev

Write-Verbose "Creating a New Azure Resource Group $RG"
New-AzureRmResourceGroup -Name $RG -Location $Location

Write-Verbose "Creating a Virtual Network $VNetName within the Resource Group $RG"
New-AzureRmVirtualNetwork -ResourceGroupName $RG -Name $VNetName -AddressPrefix $VNetPrefix -Location $Location -DnsServer $DNS01,$DNS02

$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $RG -Name $VNetName

Write-Verbose "Creating Subnet $SubNet01Name within $VNetName "
Add-AzureRmVirtualNetworkSubnetConfig -Name $SubNet01Name  -VirtualNetwork $vnet -AddressPrefix $SubNet01Prefix

Write-Verbose "Creating Subnet $SubNet02Name within $VNetName "
Add-AzureRmVirtualNetworkSubnetConfig -Name $SubNet02Name  -VirtualNetwork $vnet -AddressPrefix $SubNet02Prefix

Write-Verbose "Creating Subnet $SubNet03Name within $VNetName "
Add-AzureRmVirtualNetworkSubnetConfig -Name $SubNet03Name  -VirtualNetwork $vnet -AddressPrefix $SubNet03Prefix

Write-Verbose "Creating Subnet $GWSubName within $VNetName "
Add-AzureRmVirtualNetworkSubnetConfig -Name $GWSubName  -VirtualNetwork $vnet -AddressPrefix $GWSubPrefix

Write-Verbose "Save Changes To $VNetName in Azure"
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet
Originally Posted on December 29, 2016
Last Updated on October 26, 2025
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.