You can efficiently retrieve the Azure subscription name in Azure PowerShell primarily by using the Get-AzSubscription
cmdlet. This powerful cmdlet allows you to list all subscriptions that your current Azure account has access to, providing key details including the subscription name.
The Get-AzSubscription Cmdlet
The Get-AzSubscription
cmdlet is your primary tool for querying Azure subscription information. It fetches details for all subscriptions accessible by the authenticated account, returning essential properties such as the Subscription ID, the Subscription Name, and the Home Tenant associated with each subscription. This comprehensive output makes it straightforward to identify and manage your Azure environments.
For more details, you can refer to the official documentation for Get-AzSubscription.
Retrieving All Subscription Names
To see the names of all subscriptions your account can access, simply execute the Get-AzSubscription
cmdlet:
Get-AzSubscription
This command will output a list of subscriptions, each with its associated properties. The Name
property in the output represents the subscription name.
Example Output:
Name Id TenantId State
---- -- -------- -----
My Development Subscription xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy Enabled
Production Environment zzzzzzzz-zzzz-zzzz-zzzzz-zzzzzzzzzzzz yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy Enabled
Targeting Specific Subscription Names
Often, you might only need the subscription name or wish to extract it for further processing. There are several ways to achieve this:
1. Selecting Only the Name Property
To display only the subscription names, you can pipe the output of Get-AzSubscription
to Select-Object
and specify the Name
property:
Get-AzSubscription | Select-Object Name
This will produce a cleaner output showing only the subscription names.
2. Accessing the Name Property Directly
If you have only one subscription or want to access the name of the first subscription returned, you can directly access the Name
property:
(Get-AzSubscription).Name
If multiple subscriptions are returned, this will provide an array of all subscription names. For example, if you want the name of the first subscription:
(Get-AzSubscription)[0].Name
3. Getting the Name of the Currently Active Subscription
When working in Azure PowerShell, you're usually operating within the context of a specific subscription. To find the name of the currently active or selected subscription, use the Get-AzContext
cmdlet and access its Subscription.Name
property:
(Get-AzContext).Subscription.Name
This is particularly useful when you need to confirm which subscription you are working with before executing commands.
4. Filtering Subscriptions by Name
If you know part of the subscription name or want to find a specific one, you can filter the results:
Get-AzSubscription | Where-Object { $_.Name -like "My Development*" }
This command would retrieve any subscription whose name starts with "My Development".
Understanding Key Subscription Properties
When you run Get-AzSubscription
, it provides several important properties. Here's a brief overview of the most relevant ones for identification and management:
Property Name | Description |
---|---|
Name |
The user-friendly name of the Azure subscription. |
Id |
The unique GUID (Globally Unique Identifier) of the subscription. |
TenantId |
The unique GUID of the Azure Active Directory tenant the subscription belongs to. |
State |
The current status of the subscription (e.g., Enabled , Disabled ). |
Managing Your Active Subscription
Once you've identified a subscription by its name, you can use Set-AzContext
to switch your active working context to that subscription, allowing subsequent PowerShell commands to target it:
Set-AzContext -SubscriptionName "My Production Environment"
Or, using the subscription ID:
Set-AzContext -SubscriptionId "zzzzzzzz-zzzz-zzzz-zzzzz-zzzzzzzzzzzz"
By leveraging Get-AzSubscription
, you gain full control over identifying and interacting with your Azure subscriptions in PowerShell, making administrative tasks more efficient and precise.