New-AzContext Cmdlet

Synopsis

Creates a new Azure context. This cmdlet is primarily used to create a context based on an Azure Active Directory principal (service principal or user) or a management certificate. Use Connect-AzAccount to connect to Azure. You can then use Get-AzContext to view the current context and Set-AzContext to switch to a different context.

Syntax

Parameter Description
-TenantId The Azure Active Directory tenant ID.
-Tenant The Azure Active Directory tenant name or domain name.
-SubscriptionId The subscription ID.
-Subscription The subscription name.
-EnvironmentName The name of the Azure cloud environment (e.g., AzureChinaCloud, AzureUSGovernment).
-CertificateThumbprint The thumbprint of a management certificate.
-ApplicationId The application ID of a service principal.
-Credential A PowerShell credential object.
-Force Do not prompt for confirmation.
-WhatIf Shows what would happen if the cmdlet runs.
-Confirm Prompts you for confirmation before running the cmdlet.

Parameters

The following parameters are available:

Parameter Type Description
-TenantId System.String Specifies the Azure Active Directory tenant ID.
-Tenant System.String Specifies the Azure Active Directory tenant name or domain name.
-SubscriptionId System.String Specifies the subscription ID.
-Subscription System.String Specifies the subscription name.
-EnvironmentName System.String Specifies the name of the Azure cloud environment. Accepts values such as:
  • AzureCloud (default)
  • AzureChinaCloud
  • AzureUSGovernment
  • AzureGermanCloud
-CertificateThumbprint System.String Specifies the thumbprint of a management certificate. This is used for certificate-based authentication.
-ApplicationId System.String Specifies the application ID of a service principal. This is used for service principal authentication.
-Credential System.Management.Automation.PSCredential Specifies a PowerShell credential object. This is typically obtained using the Get-Credential cmdlet.
-Force System.Management.Automation.SwitchParameter Do not prompt for confirmation. When this parameter is used, the cmdlet will not ask you for confirmation.
-WhatIf System.Management.Automation.SwitchParameter Shows what would happen if the cmdlet runs. For example, this parameter can be used with Test-Connection to see the network packets that would be sent without actually sending them.
-Confirm System.Management.Automation.SwitchParameter Prompts you for confirmation before running the cmdlet. The default value is $true.

Examples

Example 1: Create a context with a specific subscription and tenant
PS C:\> New-AzContext -TenantId "xxxx-xxxx-xxxx-xxxx" -SubscriptionId "yyyy-yyyy-yyyy-yyyy"

This command creates a new context using the provided Tenant ID and Subscription ID.

Example 2: Create a context using a service principal and tenant name
PS C:\> $cred = Get-Credential "my-service-principal"
PS C:\> New-AzContext -Tenant "mycompany.onmicrosoft.com" -ApplicationId "aaaa-aaaa-aaaa-aaaa" -Credential $cred

This command creates a new context for a service principal, using the tenant name and application ID, and prompts for the service principal's password.

Example 3: Create a context for the Azure Government cloud
PS C:\> New-AzContext -EnvironmentName AzureUSGovernment -SubscriptionId "zzzz-zzzz-zzzz-zzzz"

This command creates a new context for the Azure US Government cloud with the specified subscription ID.

Notes

The New-AzContext cmdlet is a lower-level cmdlet for managing Azure contexts. For most common scenarios, you should use Connect-AzAccount, which handles authentication and context creation automatically.

A context consists of a subscription, a tenant, and an account. When you connect to Azure using Connect-AzAccount, it creates and sets the current context. You can view the current context using Get-AzContext.

If you have multiple subscriptions, you can use Set-AzContext to switch to a different subscription without reconnecting.