Set-AzContext

In this Azure PowerShell article, we will discuss the syntax and usage of the Set-AzContext PowerShell command. How to set Az contexts using Azure PowerShell command Set-AzContext.

Set-AzContext

The Set-AzContext PowerShell command can help you to set the subscription, tenant, and environments for different PowerShell commands to be used in your current PowerShell session.

Syntax of Set-AzContext

Below is the syntax of the Set-AzContext PowerShell command.

Set-AzContext
Set-AzContext [-Subscription] <String>
Set-AzContext
 [-Tenant <String>]
 [-Subscription] <String>
Set-AzContext
 -Tenant <String>

Let’s discuss a few examples of how to use the Set-AzContext PowerShell command.

Set-AzContext example

Example-1:

You can execute the PowerShell command below to set the specified subscription context.

Set-AzContext -Subscription '1cdf4300-dee5-4518-9c9c-feaa72a5cbd1'

After executing the above PowerShell command, I got the below-expected output

PS C:\WINDOWS\system32>  Set-AzContext -Subscription '1cdf4300-dee5-4518-9c9c-feaa72a5cbd1'

Name                                     Account                      SubscriptionName             Environment                  TenantId                    
----                                     -------                      ----------------             -----------                  --------                    
Visual Studio Enterprise (1cdf4300-de... fewlines4biju@hotmail.com    Visual Studio Enterprise     AzureCloud                   5d9d690a-0310-474d-ae8b-4...

You can check the same output in the below screenshot.

Set-AzContext

Example-2

You can also use the Subscription name parameter and the Set-AzContext PowerShell command.

Set-AzContext -SubscriptionName 'Visual Studio Enterprise'

After executing the above command, we got the expected output as shown below.

set-azcontext subscription

Example-3

You can also use the Tenant parameter along with the Set-AzContext PowerShell command.

Set-AzContext -Tenant '5d9d690a-0310-474d-ae8b-42df2d549228'

After executing the above command, I got the expected output below

How to change Azure Subscription in PowerShell

You may also like following the articles below

Wrapping Up

In this Azure PowerShell article, we discussed the syntax and usage of the Set-AzContext PowerShell command with examples of how to use this command.