Syntax error How to retrieve the Azure VM Subscription Name using PowerShell?

How to retrieve the Azure VM Subscription Name using PowerShell?



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.

Updated on: 2021-05-17T12:07:25+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements