Azure PowerShell Reference

Networking Cmdlets

Set-AzVirtualNetworkSubnetConfig

Description

Modifies the configuration of a virtual network subnet.

The Set-AzVirtualNetworkSubnetConfig cmdlet modifies the configuration of a subnet within an Azure virtual network.

Synopsis

Set-AzVirtualNetworkSubnetConfig [-Name] <String> [-AddressPrefix] <String> [-NetworkSecurityGroup <NetworkSecurityGroup>] [-RouteTable <RouteTable>] [-Delegation <PSDelegation>] [-NatGateway <NatGateway>] [-ServiceEndpoint <String[]>] [-PrivateEndpoint <PrivateEndpoint[]>] [-PrivateLinkService <PrivateLinkService[]>] [-ApplicationGateway <ApplicationGateway>] [-Delegation ] [-ServiceEndpointPolicy <ServiceEndpointPolicy[]>] [-IpAllocationMethod <IpAllocationMethod>] [-IpAddress <String>] [-IpTag <PSIpTag[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

Syntax

Set-AzVirtualNetworkSubnetConfig -Name <String> -AddressPrefix <String> [-NetworkSecurityGroup <NetworkSecurityGroup>] [-RouteTable <RouteTable>] [-Delegation <PSDelegation>] [-NatGateway <NatGateway>] [-ServiceEndpoint <String[]>] [-PrivateEndpoint <PrivateEndpoint[]>] [-PrivateLinkService <PrivateLinkService[]>] [-ApplicationGateway <ApplicationGateway>] [-ServiceEndpointPolicy <ServiceEndpointPolicy[]>] [-IpAllocationMethod <IpAllocationMethod>] [-IpAddress <String>] [-IpTag <PSIpTag[]>] [-WhatIf] [-Confirm] [<CommonParameters>]
Set-AzVirtualNetworkSubnetConfig -Subnet <PSVirtualNetworkSubnetConfig> [-NetworkSecurityGroup <NetworkSecurityGroup>] [-RouteTable <RouteTable>] [-Delegation <PSDelegation>] [-NatGateway <NatGateway>] [-ServiceEndpoint <String[]>] [-PrivateEndpoint <PrivateEndpoint[]>] [-PrivateLinkService <PrivateLinkService[]>] [-ApplicationGateway <ApplicationGateway>] [-ServiceEndpointPolicy <ServiceEndpointPolicy[]>] [-IpAllocationMethod <IpAllocationMethod>] [-IpAddress <String>] [-IpTag <PSIpTag[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

Parameters

Name Type Description
-Name String Specifies the name of the subnet to configure.
-AddressPrefix String Specifies the address space for the subnet, in CIDR notation.
-NetworkSecurityGroup NetworkSecurityGroup Specifies the network security group to associate with the subnet.
-RouteTable RouteTable Specifies the route table to associate with the subnet.
-Delegation PSDelegation Specifies a delegation for the subnet, allowing a specific Azure service to manage resources within it.
-NatGateway NatGateway Specifies the NAT gateway to associate with the subnet.
-ServiceEndpoint String[] Specifies the service endpoints to enable for the subnet.
-PrivateEndpoint PrivateEndpoint[] Specifies private endpoints associated with the subnet.
-PrivateLinkService PrivateLinkService[] Specifies private link services associated with the subnet.
-ApplicationGateway ApplicationGateway Specifies the application gateway to associate with the subnet.
-ServiceEndpointPolicy ServiceEndpointPolicy[] Specifies service endpoint policies to apply to the subnet.
-IpAllocationMethod IpAllocationMethod Specifies the IP allocation method for the subnet.
-IpAddress String Specifies the IP address for the subnet.
-IpTag PSIpTag[] Specifies IP tags for the subnet.
-Subnet PSVirtualNetworkSubnetConfig Specifies a subnet configuration object. This parameter is used when piping a subnet object to the cmdlet.
-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: Modifying a subnet's address prefix

This example updates the address prefix of a subnet named "MySubnet" within the virtual network "MyVNet".

$vnet = Get-AzVirtualNetwork -Name "MyVNet" -ResourceGroupName "MyResourceGroup"
Set-AzVirtualNetworkSubnetConfig -Name "MySubnet" -VirtualNetwork $vnet -AddressPrefix "10.0.1.0/24"
Set-AzVirtualNetwork -VirtualNetwork $vnet

Example 2: Associating a Network Security Group with a subnet

This example associates an existing Network Security Group with a subnet.

$vnet = Get-AzVirtualNetwork -Name "MyVNet" -ResourceGroupName "MyResourceGroup"
$nsg = Get-AzNetworkSecurityGroup -Name "MyNsg" -ResourceGroupName "MyResourceGroup"
Set-AzVirtualNetworkSubnetConfig -Name "MySubnet" -VirtualNetwork $vnet -NetworkSecurityGroup $nsg
Set-AzVirtualNetwork -VirtualNetwork $vnet

Example 3: Piping a subnet object

This example demonstrates using the pipeline to modify a subnet's properties.

Get-AzVirtualNetwork -Name "MyVNet" -ResourceGroupName "MyResourceGroup" | Get-AzVirtualNetworkSubnetConfig -Name "MySubnet" | Set-AzVirtualNetworkSubnetConfig -AddressPrefix "10.0.2.0/24" | Set-AzVirtualNetwork

Notes

  • To modify a subnet, you must first retrieve the virtual network object, then modify the subnet configuration within that object, and finally update the virtual network.
  • The -VirtualNetwork parameter is used to specify the virtual network containing the subnet.
  • The -Subnet parameter can be used when piping a subnet object from Get-AzVirtualNetworkSubnetConfig.