How to retrieve the Azure VM Subscription Name using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

Once you are connected to the Azure Account, there are possibilities that the Get-AzVM will display the VMs from all the Azure Subscriptions.

To find the specific Azure VM Subscription name, we will first retrieve the details of the VM using the Get-AzVM and it has an ID property that contains the Subscription ID and from the subscription ID property, we can retrieve the name of the Azure Subscription Name.

PS C:\> $azvm = Get-AzVM -Name TestMachine2k16
PS C:\> $subid = ($azvm.Id -split '/')[2]
PS C:\> (Get-AzSubscription -SubscriptionId $subid).Name

The above will retrieve the name of the subscription.

Published on 17-May-2021 12:04:34