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 = ""

#Subnet Configurations
$websubnet = New-AzVirtualNetworkSubnetConfig -Name "PIT-Web" -AddressPrefix ""
$appsubnet = New-AzVirtualNetworkSubnetConfig -Name "PIT-App" -AddressPrefix ""
$dbsubnet = New-AzVirtualNetworkSubnetConfig -Name "PIT-Data" -AddressPrefix ""

#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 "" `
  -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: