Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Add Subnets to Existing Azure vNet Using PowerShell

August 12, 2022
in Blog, Microsoft Azure, Powershell
0
ADVERTISEMENT

Table of Contents

Adding and removing subnets using the GUI is simple however when it comes to automating Azure and keeping all changes in a source control repository using PowerShell, Ansible or Terraform require a bit of coding.

Bg1630

Microsoft Virtual Network

Azure vNet is a cloud service that enables you to create a private network in the Microsoft Azure cloud. This can be used to connect your on-premises networks with Azure or to create a separate, isolated network for your applications and resources.

The Microsoft Virtual Network supports full Layer 3 connectivity between subnets within different virtual networks. This means that a subnet in one virtual network can be connected to a subnet in another virtual network over the public Internet.

Connect to Microsoft Azure PowerShell

First, you need connect to Microsoft Azure PowerShell, there’re two ways to run PowerShell commands in Microsoft Azure:

Method 1: From Azure PowerShell modules installed in a computer.

Bg1624

Method 2: From Azure Cloud Shell using browsers.

Bg1625
ADVERTISEMENT

Add Subnets to Existing Azure vNet

Modifying an existing Azure vNet with PowerShell is not a simple process sadly. In order to modify an existing vNet, we need first recreate the vNet with the changes in variables via PowerShell and then apply the changes.

So first before we make changes, we need to make sure we know what we have in the current vNet.

Get-AzVirtualNetwork -ResourceGroupName 'backup-rg'

Name                   : backup-vnet
ResourceGroupName      : Backup-RG
Location               : westus
AddressSpace           : {
                           "AddressPrefixes": [
                             "172.16.0.0/16"
                           ]
                         }
Subnets                : [
                           {
                             "Delegations": [],
                             "Name": "default",
                             "Etag": "W/\"76aba1ae-2e3c-4bb8-8cdb-d9a36f7b9009\"",
                             "Id": "/subscriptions/089d4ee9-0b8d-4bac-ab78-c120f115e79e/resourceGroups/Backup-RG/provid
                         ers/Microsoft.Network/virtualNetworks/backup-vnet/subnets/default",
                             "AddressPrefix": [
                               "172.16.1.0/24"
                             ],
                             "IpConfigurations": [],
....
                           }
                         ]

The default subnet is already exist in the vNet however we still need to add it to a variable to update in next step.

$default = New-AzVirtualNetworkSubnetConfig -Name default -AddressPrefix "172.16.1.0/24"

In the cmdlets below, we’re going to add two subnets ( servers and dmz) to our existing vNet.

$servers = New-AzVirtualNetworkSubnetConfig -Name servers -AddressPrefix "172.16.2.0/24"
$dmz = New-AzVirtualNetworkSubnetConfig -Name dmz -AddressPrefix "172.16.3.0/24"

The updated vNet with the modified subnets is called updatedvnet.

$updatedvnet = New-AzVirtualNetwork -Name 'backup-vnet' `
-ResourceGroupName 'Backup-RG' `
-Location 'westus' `
-AddressPrefix "172.16.0.0/16" `
-Subnet $default, $dmz, $servers `
-Force:$true

To wrap up all the changes and apply them using PowerShell I’m using the last line in the below code and use the Set-AzVirtualNetwork command.

$default = New-AzVirtualNetworkSubnetConfig -Name default -AddressPrefix "172.16.1.0/24"
$servers = New-AzVirtualNetworkSubnetConfig -Name server -AddressPrefix "172.16.2.0/24"
$dmz = New-AzVirtualNetworkSubnetConfig -Name dmz -AddressPrefix "172.16.3.0/24"
$updatedvnet = New-AzVirtualNetwork -Name 'backup-vnet' `
-ResourceGroupName 'Backup-RG' `
-Location 'westus' `
-AddressPrefix "172.16.0.0/16" `
-Subnet $default, $dmz, $servers `
-Force:$true
$updatedvnet | Set-AzVirtualNetwork
Bg1629
ADVERTISEMENT
5/5 - (1 vote)
Previous Post

How to Hide The Yellow Warning Output in PowerShell

Next Post

How to Get Creation Date of Microsoft 365 Users using PowerShell

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory