Set-AzVirtualNetwork

Overview

Modifies an existing virtual network (VNet). This cmdlet can be used to add or remove subnets, or to change the address space.

SYNTAX


Set-AzVirtualNetwork
   [-VirtualNetwork] <PSVirtualNetworkIdentity>
   [-AddressPrefix <String[]>]
   [-DhcpOptions <PSDhcpOptions>]
   [-DnsServer <String[]>]
   [-Location <String>]
   [-Name <String>]
   [-ResourceGroupName <String>]
   [-Tag <Hashtable>]
   [-Force]
   [-AsJob]
   [-DefaultProfile <IAzureContextContainer>]
   [-WhatIf]
   [-Confirm]
   [ <CommonParameters> ]
                

PARAMETERS

Parameter Type Description
-VirtualNetwork Microsoft.Azure.Commands.Network.Models.PSVirtualNetworkIdentity Specifies the virtual network object that this cmdlet modifies. You can use the Get-AzVirtualNetwork cmdlet to retrieve the object.
-AddressPrefix String[] Specifies the IP address prefixes for the virtual network.
-DhcpOptions PSDhcpOptions Specifies the DHCP options for the virtual network. This parameter contains the DNS server IP address configuration.
-DnsServer String[] Specifies the DNS server IP addresses for the virtual network.
-Location String Specifies the location of the virtual network.
-Name String Specifies the name of the virtual network.
-ResourceGroupName String Specifies the name of the resource group that the virtual network belongs to.
-Tag Hashtable Specifies tags for the virtual network. Tags are key-value pairs that help you categorize and organize resources.
-Force SwitchParameter Forces the command to run without asking for confirmation.
-AsJob SwitchParameter Run cmdlet in the background.
-DefaultProfile IAzureContextContainer The credentials, account, tenant, and subscription that are used for communication with Azure.
-WhatIf SwitchParameter Shows what would happen if the cmdlet runs. The cmdlet is not run.
-Confirm SwitchParameter Prompts you for confirmation before running the cmdlet.

EXAMPLES

Example 1: Add a subnet to a virtual network


# Get the virtual network
$vnet = Get-AzVirtualNetwork -Name "MyVirtualNetwork" -ResourceGroupName "MyResourceGroup"

# Add a new subnet
Add-AzVirtualNetworkSubnetConfig -Name "NewSubnet" -AddressPrefix "10.0.1.0/24" -VirtualNetwork $vnet

# Update the virtual network with the new subnet
Set-AzVirtualNetwork -VirtualNetwork $vnet
                

Example 2: Change the address prefix of a virtual network


# Get the virtual network
$vnet = Get-AzVirtualNetwork -Name "MyVirtualNetwork" -ResourceGroupName "MyResourceGroup"

# Change the address prefix
$vnet.AddressSpace.AddressPrefixes.Clear()
$vnet.AddressSpace.AddressPrefixes.Add("10.0.0.0/16")

# Update the virtual network
Set-AzVirtualNetwork -VirtualNetwork $vnet
                

NOTES

Important Notes

When modifying the address space of a virtual network, ensure that the new address ranges do not overlap with existing subnets. This cmdlet performs modifications in place, so it's recommended to retrieve the virtual network object first using Get-AzVirtualNetwork, modify it in memory, and then use Set-AzVirtualNetwork to apply the changes.

RELATED CMDLETS