Recently, I got the requirement to get the subnet id in Azure using PowerShell. This article will help you to get subnet id in Azure PowerShell.
How To Get Subnet ID In Azure PowerShell
To get Subnet ID in Azure PowerShell, we can use the Get-AzVirtualNetworkSubnetConfig command along with the complete script below.
$mySubnet = New-AzVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix "10.0.1.0/24"
$myvirtualNetwork = New-AzVirtualNetwork -Name test -ResourceGroupName MyNewResGrp -Location EastUS -AddressPrefix "10.0.0.0/16" -Subnet $mySubnet
$subnetConfig = Get-AzVirtualNetworkSubnetConfig -Name mySubnet -VirtualNetwork $myvirtualNetwork
$subnetId = $subnetConfig.Id
Write-Output "Subnet ID: $subnetId" After executing the above query, I got the expected output, as shown below.
Subnet ID: /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/MyNewResGrp/providers/Microsoft
.Network/virtualNetworks/test/subnets/mySubnetYou can see the same output as shown in the screenshot below.

OR, You can also use the Get-AzVirtualNetwork command along with the script below to achieve this.
$subnetId = (Get-AzVirtualNetwork -Name "test" -ResourceGroupName "MyNewResGrp" |
Select-Object -ExpandProperty Subnets |
Where-Object { $_.Name -eq "mySubnet" }).Id
Write-Output "Subnet ID: $subnetId"After executing the above query, I got the expected output, as shown in the screenshot below.

OR, We can also use the below query for this purpose.
$vnet = Get-AzVirtualNetwork -Name "test" -ResourceGroupName "MyNewResGrp"
$subnets = $vnet.Subnets | Select-Object Id
Write-Output "Subnet ID: $subnets"After executing the above query, I got the expected output, as shown in the screenshot below.

You may also like following the articles below.
- How to delete Virtual Network in Azure
- Get-AzVirtualNetwork
- How To Attach Network Interface To Azure VM
- How To Attach Network Interface To Azure VM

I am Rajkishore, and I am a Microsoft Certified IT Consultant. I have over 14 years of experience in Microsoft Azure and AWS, with good experience in Azure Functions, Storage, Virtual Machines, Logic Apps, PowerShell Commands, CLI Commands, Machine Learning, AI, Azure Cognitive Services, DevOps, etc. Not only that, I do have good real-time experience in designing and developing cloud-native data integrations on Azure or AWS, etc. I hope you will learn from these practical Azure tutorials. Read more.
