Set-AzContext

Changes the current Azure PowerShell context.

Syntax

Set-AzContext
   [-TenantId ]
   [-Tenant ]
   [-SubscriptionId ]
   [-Subscription ]
   [-TenantDomainName ]
   [-TenantDisplayName ]
   [-Default]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   []

Description

The Set-AzContext cmdlet changes the current Azure PowerShell context. The context is defined by the tenant, subscription, and account that you are working with.

When you connect to Azure using Connect-AzAccount, a context is created. You can have multiple contexts and switch between them using Set-AzContext. This is particularly useful if you manage multiple Azure subscriptions or tenants.

Parameters

Name Type Description
-TenantId System.String Specifies the ID of the tenant to set as the current context.
-Tenant Microsoft.Azure.PowerShell.Cmdlets.Subscriptions.ITenant Specifies a tenant object to set as the current context.
-SubscriptionId System.String Specifies the ID of the subscription to set as the current context.
-Subscription Microsoft.Azure.PowerShell.Cmdlets.Subscriptions.ISubscription Specifies a subscription object to set as the current context.
-TenantDomainName System.String Specifies the domain name of the tenant.
-TenantDisplayName System.String Specifies the display name of the tenant.
-Default System.Management.Automation.SwitchParameter Sets the specified context as the default context.
-PassThru System.Management.Automation.SwitchParameter Returns the context object that was set.
-WhatIf System.Management.Automation.SwitchParameter Performs the cmdlet in the form of a statement, but does not execute the resources or resources.
-Confirm System.Management.Automation.SwitchParameter Prompts you for confirmation before running the cmdlet.

Examples

Example 1: Set context by Subscription ID

Set-AzContext -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Example 2: Set context by Tenant Name and make it default

Set-AzContext -TenantDomainName "contoso.com" -Default

Example 3: Get all available contexts and set a specific one

Get-AzContext | Out-Host
Set-AzContext -TenantId "yyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" -SubscriptionId "zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz"

Example 4: Using PassThru to see the context being set

Set-AzContext -SubscriptionName "MyDevelopmentSubscription" -PassThru