Powershell to dissociate nsg from subnet.

FATOBA, KIMBERLY 70 Reputation points
2023-05-26T09:11:22.3333333+00:00

Hi All:

I am running the Remove-AzNetworkSecurityGroup pwsh command to delete an NSG, this NSG is currently associated with a subnet but there is no direct access over portal to dissociate from the subnet via Azure portal, so the deletion is stuck.

Can anyone kindly provide a quick pwsh one-liner to dissociate NSG from subnet ?

Many Thanks

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
{count} votes

Answer accepted by question author
  1. GitaraniSharma-MSFT 50,176 Reputation points Microsoft Employee Moderator
    2023-05-26T10:36:37.48+00:00

    Hello @FATOBA, KIMBERLY ,

    Welcome to Microsoft Q&A Platform. Thank you for reaching out & hope you are doing well.

    I understand that you are running the "Remove-AzNetworkSecurityGroup" PowerShell command to delete an NSG, but this NSG is currently associated with a subnet, and you don't have direct access to Azure portal to dissociate it from the subnet, so the deletion is stuck. You need the PowerShell command to dissociate NSG from subnet.

    You can find the PowerShell commands for dissociating a network security group from a subnet in the below doc:

    https://learn.microsoft.com/en-us/azure/virtual-network/manage-network-security-group?tabs=network-security-group-powershell#associate-or-dissociate-a-network-security-group-to-or-from-a-subnet

    Instead of declaring the NSG, you can add $null to the Set-AzVirtualNetworkSubnetConfig command to disassociate the NSG as below:

    ## Place the virtual network configuration into a variable ##
    
    $vnet = Get-AzVirtualNetwork -Name vnetname -ResourceGroupName rgname
    
    ## Update the subnet configuration to disassociate the NSG using $null ##
    
    Set-AzVirtualNetworkSubnetConfig -Name subnetname -VirtualNetwork $vnet -AddressPrefix 10.0.0.0/24 -NetworkSecurityGroup $null
    
    ## Update the virtual network ##
    
    Set-AzVirtualNetwork -VirtualNetwork $vnet
    

    I tried it in my lab and it works as you can see from the below screenshot:

    enter image description here

    Then you can delete the NSG using the below command:

    Remove-AzNetworkSecurityGroup -Name "nsgname" -ResourceGroupName "rgname"
    

    Kindly let us know if the above helps or you need further assistance on this issue.


    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


1 additional answer

Sort by: Most helpful
  1. TP 145.2K Reputation points Volunteer Moderator
    2023-05-26T09:15:13.7966667+00:00

    Hi,

    If you navigate to the NSG in the Azure portal, open Subnets blade, you can click on the context menu (three dots) to the right of the subnet and choose Disassociate.

    Please click Accept Answer if the above was helpful.

    Thanks.

    -TP


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.