How to create virtual network in Azure using PowerShell

Step 01: Open PowerShell on your system as shown below

Step 02: Connect to your Azure account using the command “Connect-AzAccount

Step 03: Find the below command line script to run and create the virtual networks.

The below example is used only for demonstration. you may edit this script based on your requirement for vNET creation.

#Resource Group and Location
$rg = "PIT-Network"
$location = "eastus"

#VNET Name and Address Space
$VNETName = "PIT-VNET-PShell"
$VNETAddressSpace = "10.3.0.0/22"

#Subnet Configurations
$websubnet = New-AzVirtualNetworkSubnetConfig -Name "PIT-Web" -AddressPrefix "10.3.0.0/24"
$appsubnet = New-AzVirtualNetworkSubnetConfig -Name "PIT-App" -AddressPrefix "10.3.1.0/24"
$dbsubnet = New-AzVirtualNetworkSubnetConfig -Name "PIT-Data" -AddressPrefix "10.3.2.0/24"

#Create Resource Group
New-AzResourceGroup -Name $rg -Location $location


#Create VNET and Subnets
$virtualNetwork = New-AzVirtualNetwork -Name $VNETName -ResourceGroupName $rg `
    -Location $location -AddressPrefix $VNETAddressSpace -Subnet $websubnet,$appsubnet

#Add Additional Subnet
$subnetConfig = Add-AzVirtualNetworkSubnetConfig `
  -Name "AddSubnet" `
  -AddressPrefix "10.3.4.0/24" `
  -VirtualNetwork $virtualNetwork

#Write the changes to the VNET
$virtualNetwork | Set-AzVirtualNetwork

Step 04: Virtual network and subnets created in the above step can be verified from Azure portal as shown below.

This concludes this demonstration of creating virtual network using PowerShell command based script.

%d bloggers like this: