The ‘Set-AzContext’ command was found in the module ‘Az.Accounts’ but the module could not be loaded.

In this azure tutorial, we will discuss how to fix the error, The ‘Set-AzContext’ command was found in the module ‘Az.Accounts’, but the module could not be loaded. which comes while trying to create an Azure SQL database using PowerShell in Azure.

The ‘Set-AzContext’ command was found in the module ‘Az.Accounts’, but the module could not be loaded.

Recently while trying to create an Azure SQL database using PowerShell, I got the above error message. Below was the cmdlet I was executing for creating the AzureSQL database.

Note: This is the part of my actual script.

$MysubscriptionId = '1cdf4300-dee5-4518-9c9c-feaa72a5cbd1'
# Set subscription details
Set-AzContext -SubscriptionId $MysubscriptionId 
The 'Set-AzContext' command was found in the module 'Az.Accounts'

The exact error message is

Set-AzContext : The ‘Set-AzContext’ command was found in the module ‘Az.Accounts’, but the module could not be loaded. For more information, run ‘Import-Module Az.Accounts’.
At line:19 char:1

  • Set-AzContext -SubscriptionId $MysubscriptionId
  • ~~~~~
    • CategoryInfo : ObjectNotFound: (Set-AzContext:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

The ‘Set-AzContext’ command was found in the module ‘Az.Accounts’, but the module could not be loaded. [Solved]

To fix the above issue, you need to follow the below steps

Step-1:

Run the below command to import the Az.Accounts

C:\windows\system32> Import-Module Az.Accounts
The 'Set-AzContext' command was found in the module 'Az.Accounts'

After executing the above command, there might be a chance you will get one error like Import-Module: File C:\Program Files\WindowsPowerShell\Modules\Azure\5.3.0\.psm1 cannot be loaded because running scripts is disabled on this system.

Step-2:

Now run the below command to set the Execution policy to fix this issue

PS C:\windows\system32> Set-ExecutionPolicy RemoteSigned
The 'Set-AzContext' command was found

Step-3:

Click on the yes button from the below popup.

How to fix The 'Set-AzContext' command was found

Step-4:

Now run the below command again to import the Az.Accounts

C:\windows\system32> Import-Module Az.Accounts
The 'Set-AzContext' command was found in the module 'Az.Accounts'

Step-5:

Now, run the cmdlet again for creating the AzureSQL database(This is part of my script), you will not get any error this time.

$MysubscriptionId = '1cdf4300-dee5-4518-9c9c-feaa72a5cbd1'
# Set subscription details
Set-AzContext -SubscriptionId $MysubscriptionId 
The 'Set-AzContext' command was found in the module

Set-AzContext

The Set-AzContext Azure PowerShell cmdlet helps you to set the authentication information in the current Azure PowerShell session. The context also includes subscription, tenant, and environment information, etc.

Syntax

The syntax of the Set-AzContext is as below

Set-AzContext -SubscriptionId "Your Azure subscription ID"

Example

PS C:\WINDOWS\system32> Set-AzContext -SubscriptionId "1cdf4xxx-xxxx-4518-9c9c-feaa72a5cbd1"

Name                                     Account     Subscriptio Environmen TenantId  
                                                     nName       t                    
----                                     -------     ----------- ---------- --------  
Visual Studio Enterprise (1cdf4300-de... fewlines... Visual S... AzureCloud 5d9d690...

You can able to see the output as below

Set-AzContext

You can able to use few parameters along with the above PowerShell cmdlet and those are -Confirm, -Context, –DefaultProfile, -ExtendedProperty, -Force, -Name, -Scope, -Subscription, -Tenant, -WhatIf, etc.

Get-AzContext

The Get-AzContext Azure PowerShell cmdlet helps us to get the current metadata used to authenticate the Azure Resource manager requests. This Azure PowerShell cmdlet also gets the Azure subscription, Active Directory tenant, Active Directory account, etc. that helps the Azure Resource Manager to make the Azure manager requests.

Syntax

PS C:\WINDOWS\system32> Get-AzContext

If we will run the above PowerShell cmdlet, you can able to see the Output as below

Name                                     Account     Subscriptio Environmen TenantId  
                                                     nName       t                    
----                                     -------     ----------- ---------- --------  
Visual Studio Enterprise (1cdf4300-de... fewlines... Visual S... AzureCloud 5d9d690...
Get-AzContext

The parameters that are supported here are -DefaultProfile, -ListAvailable, -Name.

Set-AzContext : Please provide a valid tenant or a valid subscription.

Once while executing the Set-AzContext Azure PowerShell cmdlet, I got the error “Please provide a valid tenant or a valid subscription.” while I was providing the correct subscription.

The complete error message was as below

Set-AzContext : Please provide a valid tenant or a valid subscription.
At line:6 char:1
+ Set-AzContext -SubscriptionId azeq-****-****-****

Now, we will see How to fix the error “Please provide a valid tenant or a valid subscription.

Set-AzContext : Please provide a valid tenant or a valid subscription. [Solved]

I have fixed this issue by executing the below PowerShell cmdlet

Clear-AzContext
Connect-AzAccount -Tenant XXXXXX -Subscription XXXXXX

How to List down and set the Azure Subscription using Az PowerShell cmdlet

View Selected Azure Subscription

You can use the below Azure PowerShell cmdlet to view the selected Azure Subscription.

PS C:\WINDOWS\system32> Get-AzContext

You can able to see the output right here

Get-AzContext

Get the List of Azure Subscriptions

You can use the below PowerShell cmdlet to list all of your Azure Subscriptions

PS C:\WINDOWS\system32> Get-AzSubscription

You can get the output like below

PS C:\WINDOWS\system32> Get-AzSubscription

Name                     Id                                   TenantId                
----                     --                                   --------                
Visual Studio Enterprise 1cdf4300-dee5-4518-9c9c-feaa72a5cbd1 5d9d690a-0310-474d-ae...

You can able to see the output is as below.

Get the List of Azure Subscriptions

Set Azure Subscription

You can set the Azure Subscription using the below PowerShell cmdlet. Either you can use the name or ID of the Azure Subscription.

PS C:\WINDOWS\system32> Set-AzContext -SubscriptionId "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"

Or you can also use the -SubscriptionName parameter with the Set-Azcontext Azure PowerShell cmdlet.

PS C:\WINDOWS\system32> Set-AzContext -SubscriptionName "Your Azure Subscription name"

How to Set Your AzContext Interactively

Azure Contexts Powershell

When you are working with Azure, You will have multiple Azure subscriptions or accounts that you have to work with. For example, you might have a personal account, a professional or work account, or a school account. While working with PowerShell, you have to set the proper context.

Set-AzContext

You can use the Get-AzContext -List cmdlet to get the list of all your context and then you can use the Set-AzContext to set the specific context that you want to set for the current PowerShell session.

‘Connect-AzAccount’ command was found in the module ‘Az.Accounts’, but the module could not be loaded. 

While working with the Visual studio code terminal for certain requirement, I got the error ‘Connect-AzAccount’ command was found in the module ‘Az.Accounts’, but the module could not be loaded. 

Now, I have fixed the issue using the following steps

‘Connect-AzAccount’ command was found in the module ‘Az.Accounts’, but the module could not be loaded. [Solved]

I have fixed the issue following the below two steps

  1. I have executed the below PowerShell cmdlet.
set-executionpolicy remotesigned

2. The second step is, I have restarted the Visual Studio Code.

If you are getting the same error in PowerShell, Along with the first step you need to install the .NET Framework 4.7.2 or greater version on your machine to fix this issue.

The term ‘Connect-AzAccount’ is not recognized as the name of a cmdlet 

Recently, while I was trying to connect to Microsoft Azure from PowerShell by running the below command

Connect-AzAccount

You can refer The term ‘connect-azaccount’ is not recognized as the name of a cmdlet to fix the same issue.

You may like following Azure tutorials:

In this Azure tutorial, we discussed how to fix the error, The ‘Set-AzContext’ command was found in the module ‘Az. Accounts’. I hope it helped you to fix your issue.

The above steps will also fix the below issue

  • Set-AzContext Fails to Set Context to named Subscription