Authenticate with Azure PowerShell
Learn how to sign in to Azure using PowerShell so you can manage resources, automate deployments, and integrate with CI/CD pipelines.
Prerequisites
- PowerShell 7.0+ (or Windows PowerShell 5.1)
- Azure PowerShell module (
Az) installed - An Azure account or service principal with appropriate permissions
Login Methods
Interactive login (Device code)
Best for developers working locally.
Connect-AzAccount -UseDeviceAuthentication
Service Principal with Secret
Ideal for automation scripts.
$tenantId = "YOUR_TENANT_ID"
$appId = "YOUR_APP_ID"
$secret = "YOUR_CLIENT_SECRET"
Connect-AzAccount -ServicePrincipal -Tenant $tenantId -ApplicationId $appId -Credential (New-Object System.Management.Automation.PSCredential($appId,(ConvertTo-SecureString $secret -AsPlainText -Force)))
Managed Identity (Azure VM, App Service)
Used when the script runs in an Azure resource with a managed identity enabled.
Connect-AzAccount -Identity
Verification
After logging in, confirm the context:
Get-AzContext
Common Issues
| Issue | Resolution |
|---|---|
| Invalid credentials | Verify client secret or certificate details. |
| Insufficient permissions | Assign appropriate role (e.g., Contributor) to the service principal or managed identity. |
| Module not found | Install with Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force. |