How to create, configure, and manage your network security groups
At this time, NSGs can be configured and modified by using PowerShell cmdlets and REST APIs only. You cannot configure NSGs by using the Management Portal. The PowerShell cmdlets below will help you create, configure, and manage your NSGs.
Create a Network Security Group
Copy
New-AzureNetworkSecurityGroup -Name "MyVNetSG" -Location uswest `
-Label "Security group for my Vnet in West US"
Add or Update rules
Copy
Get-AzureNetworkSecurityGroup -Name "MyVNetSG" `
| Set-AzureNetworkSecurityRule -Name WEB -Type Inbound -Priority 100 `
-Action Allow -SourceAddressPrefix 'INTERNET' -SourcePortRange '*' `
-DestinationAddressPrefix '*' -DestinationPortRange '*' -Protocol TCP
Delete a rule from an NSG
Copy
Get-AzureNetworkSecurityGroup -Name "MyVNetSG" `
| Remove-AzureNetworkSecurityRule -Name WEB
Associate an NSG to a VM
Copy
Get-AzureVM -ServiceName "MyWebsite" -Name "Instance1" `
| Set-AzureNetworkSecurityGroupConfig -NetworkSecurityGroupName "MyVNetSG" `
| Update-AzureVM
View NSGs associated to a VM
Copy
Get-AzureVM -ServiceName "MyWebsite" -Name "Instance1" `
| Get-AzureNetworkSecurityGroupAssociation
Remove an NSG from a VM
Copy
Get-AzureVM -ServiceName "MyWebsite" -Name "Instance1" `
| Remove-AzureNetworkSecurityGroupConfig -NetworkSecurityGroupName "MyVNetSG" `
| Update-AzureVM
Associate an NSG to a subnet
Copy
Get-AzureNetworkSecurityGroup -Name "MyVNetSG" `
| Set-AzureNetworkSecurityGroupToSubnet -VirtualNetworkName 'VNetUSWest' `
-SubnetName 'FrontEndSubnet'
View NSGs associated to a subnet
Copy
Get-AzureNetworkSecurityGroupForSubnet -SubnetName 'FrontEndSubnet' `
-VirtualNetworkName 'VNetUSWest'
Remove an NSG from the subnet
Copy
Get-AzureNetworkSecurityGroup -Name "MyVNetSG" `
| Remove-AzureNetworkSecurityGroupFromSubnet -VirtualNetworkName 'VNetUSWest' `
-SubnetName 'FrontEndSubnet'
Delete an NSG
Copy
Remove-AzureNetworkSecurityGroup -Name "MyVNetSG"
Get the details of an NSG along with rules
Copy
Get-AzureNetworkSecurityGroup -Name "MyVNetSG" -Detailed
View all Azure PowerShell cmdlets realted to NSGs
Copy
Get-Command *azurenetworksecuritygroup*