Login-AzAccount
The Login-AzAccount cmdlet authenticates you to Azure and sets the default subscription context for subsequent Azure PowerShell commands.
Syntax
Login-AzAccount
[-SubscriptionId] <String>
[-Tenant] <String>
[-Credential] <PSCredential>
[-Environment] <String>
[-DeviceCode] <SwitchParameter>
[-UseDeviceAuthentication] <SwitchParameter>
[-Scope] <String[]>
[-SkipContextPopulation] <SwitchParameter>
[-AllowNoSubscriptions] <SwitchParameter>
[<CommonParameters>]
Parameters
| Name | Type | Description |
|---|---|---|
| -SubscriptionId | String | Specifies the subscription ID to set as the context. |
| -Tenant | String | The tenant (directory) ID or domain name. |
| -Credential | PSCredential | Credentials for username/password authentication. |
| -Environment | String | The Azure cloud environment (AzureCloud, AzureChinaCloud, etc.). |
| -DeviceCode | SwitchParameter | Use device code authentication flow. |
| -UseDeviceAuthentication | SwitchParameter | Alias for -DeviceCode. |
| -Scope | String[] | Additional scopes to request. |
| -SkipContextPopulation | SwitchParameter | Skip storing the context for the session. |
| -AllowNoSubscriptions | SwitchParameter | Allow login when the account has no subscriptions. |
Examples
Example 1: Interactive login
Login-AzAccount
Example 2: Login using a service principal
$sp = Get-Credential
Login-AzAccount -ServicePrincipal -Tenant "contoso.onmicrosoft.com" -Credential $sp
Example 3: Authenticate with device code (non‑interactive)
Login-AzAccount -DeviceCode
Notes
- If you have multiple subscriptions, use
Set-AzContext -Subscription <IdOrName>after logging in. - For automation, prefer
Connect-AzAccount -ServicePrincipalwith a certificate or secret. - The cmdlet stores a token cache in
%USERPROFILE%\.Azure\AzureRMContext.json.