Az.Accounts Module

The Az.Accounts module is the foundational module for interacting with Azure resources using Azure PowerShell. It provides cmdlets for managing your Azure account, subscriptions, and tenants, as well as managing your PowerShell session context.

This module is essential for any Azure PowerShell scripting. Without it, you cannot connect to your Azure account, select a subscription, or manage your Azure resources.

Important: The Az.Accounts module is automatically installed as part of the Az PowerShell module. It is recommended to install the entire Az module for comprehensive Azure management.

Key Cmdlets

The Az.Accounts module contains several core cmdlets for account management:

Get-AzContext

Retrieves the current Azure PowerShell context. This includes information about the currently connected account, tenant, and subscription.

Get-AzContext

Example:

PS C:\> Get-AzContext

Name                                     Account                                  SubscriptionName                         TenantId                                 Environment
----                                     -------                                  ----------------                         --------                                 -----------
MyAzureSubscription                      user@example.com                         MyDevelopmentSubscription                xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx     AzureCloud

Set-AzContext

Sets the Azure PowerShell context. This allows you to switch between different accounts, subscriptions, or tenants within your PowerShell session.

Parameters:

Parameter Type Description
-TenantId [string] Specifies the ID of the tenant to set as the context.
-SubscriptionId [string] Specifies the ID of the subscription to set as the context.
-SubscriptionName [string] Specifies the name of the subscription to set as the context.
-Tenant [Microsoft.Azure.Commands.Common.Authentication.Models.IAzTenant] Specifies a tenant object to set as the context.
-Subscription [Microsoft.Azure.Commands.Common.Authentication.Models.IAzSubscription] Specifies a subscription object to set as the context.
Set-AzContext -SubscriptionId "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"

Example:

PS C:\> Set-AzContext -SubscriptionName "Production"
PS C:\> Get-AzContext

Name                                     Account                                  SubscriptionName                         TenantId                                 Environment
----                                     -------                                  ----------------                         --------                                 -----------
Production                               user@example.com                         Production                               xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx     AzureCloud

Disconnect-AzAccount

Logs out of the current Azure account session. This disconnects you from all Azure subscriptions associated with the current context.

Disconnect-AzAccount

Example:

PS C:\> Disconnect-AzAccount
PS C:\> Get-AzContext

# No context information displayed, indicating disconnection.

Get-AzTenant

Retrieves a list of all Azure tenants that the current user has access to.

Get-AzTenant

Example:

PS C:\> Get-AzTenant

Id                                   State
--                                   -----
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Active
yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy Active

Get-AzSubscription

Retrieves a list of all Azure subscriptions that the current user has access to. You can filter by tenant or by setting the context.

Get-AzSubscription

Example:

PS C:\> Get-AzSubscription

Id                                   Name                                     TenantId                                 State
--                                   ----                                     --------                                 -----
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa Development                              xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx     Enabled
bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb Production                               xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx     Enabled
Tip: You can combine Get-AzTenant and Get-AzSubscription with Set-AzContext to easily switch between different Azure environments and subscriptions.