To create a VNet by using PowerShell, follow the steps below.
-
If you have never used Azure PowerShell, see How to Install and Configure Azure PowerShell and follow the instructions all the way to the end to sign into Azure and select your subscription.
-
If necessary, create a new resource group, as shown below. For our 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/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TestRG
-
Create a new VNet named TestVNet, as shown below.
New-AzureRmVirtualNetwork -ResourceGroupName TestRG -Name TestVNet ` -AddressPrefix 192.168.0.0/16 -Location centralus
Expected output:
Name : TestVNet ResourceGroupName : TestRG Location : centralus Id : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet Etag : W/"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ProvisioningState : Succeeded Tags : AddressSpace : { "AddressPrefixes": [ "192.168.0.0/16" ] } DhcpOptions : {} Subnets : [] VirtualNetworkPeerings : []
-
Store the virtual network object in a variable, as shown below.
$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.
-
Add a subnet to the new VNet variable, as shown below.
Add-AzureRmVirtualNetworkSubnetConfig -Name FrontEnd ` -VirtualNetwork $vnet -AddressPrefix 192.168.1.0/24
Expected output:
Name : TestVNet ResourceGroupName : TestRG Location : centralus Id : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet Etag : W/"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ProvisioningState : Succeeded Tags : AddressSpace : { "AddressPrefixes": [ "192.168.0.0/16" ] } DhcpOptions : {} Subnets : [ { "Name": "FrontEnd", "AddressPrefix": "192.168.1.0/24" } ] VirtualNetworkPeerings : []
-
Repeat step 5 above for each subnet you want to create. The command below creates the BackEnd subnet for our scenario.
Add-AzureRmVirtualNetworkSubnetConfig -Name BackEnd ` -VirtualNetwork $vnet -AddressPrefix 192.168.2.0/24
-
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 Set-AzureRmVirtualNetwork cmdlet, as shown below.
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet
Expected output:
Name : TestVNet ResourceGroupName : TestRG Location : centralus Id : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet Etag : W/"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ProvisioningState : Succeeded Tags : AddressSpace : { "AddressPrefixes": [ "192.168.0.0/16" ] } DhcpOptions : { "DnsServers": [] } Subnets : [ { "Name": "FrontEnd", "Etag": "W/\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", "Id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet/subnets/FrontEnd", "AddressPrefix": "192.168.1.0/24", "IpConfigurations": [], "ProvisioningState": "Succeeded" }, { "Name": "BackEnd", "Etag": "W/\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", "Id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet/subnets/BackEnd", "AddressPrefix": "192.168.2.0/24", "IpConfigurations": [], "ProvisioningState": "Succeeded" } ] VirtualNetworkPeerings : []