The ‘New-AzSqlServer’ command was found in the module ‘Az.Sql’, but the module could not be loaded

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

We will also discuss how to fix the below errors

  • The ‘New-AzSqlServerFirewallRule’ command was found in the module ‘Az.Sql’, but the module could not be loaded.
  • The ‘New-AzSqlDatabase’ command was found in the module ‘Az.Sql’, but the module could not be loaded.

The ‘New-AzSqlServer’ command was found in the module ‘Az.Sql’, 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.

# Creation of server
$Myserver = New-AzSqlServer -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -Location $Mylocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $NewadminSqlLogin, $(ConvertTo-SecureString -String $Newpassword -AsPlainText -Force))
The 'New-AzSqlServer' command was found in the module 'Az.Sql', but the module could not be loaded.

Note: This is the part of my actual script.

The exact error message was

New-AzSqlServer: The ‘New-AzSqlServer’ command was found in the module ‘Az.Sql’, but the module could not be loaded. For more information, run ‘Import-Module Az.Sql’.
At line:25 char:13

  • $Myserver = New-AzSqlServer -ResourceGroupName $MyresourceGroupName `
  • ~~~
    • CategoryInfo : ObjectNotFound: (New-AzSqlServer:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

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

Run the below command to import the Az.Sql

PS C:\windows\system32> Import-Module Az.Sql
The 'New-AzSqlServer' command was found in the module 'Az.Sql'

After importing the Az.Sql module, run the below cmdlet again to create the SQL server. Now you should not get any error

# Creation of server
$Myserver = New-AzSqlServer -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -Location $Mylocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $NewadminSqlLogin, $(ConvertTo-SecureString -String $Newpassword -AsPlainText -Force))
The 'New-AzSqlServer' command was found in the module 'Az.Sql'

The ‘New-AzSqlServerFirewallRule’ command was found in the module ‘Az.Sql’, but the module could not be loaded.

I also got the error “The ‘New-AzSqlServerFirewallRule’ command was found in the module ‘Az.Sql’, but the module could not be loaded”. which comes while trying to create an Azure SQL database using PowerShell in Azure.

I was using the below cmdlet

$MyserverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
    -ServerName $NewserverName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $NewstartIp -EndIpAddress $NewendIp
The 'New-AzSqlServerFirewallRule' command was found in the module 'Az.Sql'

The actual error was

New-AzSqlServerFirewallRule: The ‘New-AzSqlServerFirewallRule’ command was found in the module ‘Az.Sql’, but the module could not be loaded. For more information, run ‘Import-Module Az.Sql’.
At line:31 char:23

  • $serverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName …
  • ~~~~~~~
    • CategoryInfo : ObjectNotFound: (New-AzSqlServerFirewallRule:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

To fix this issue, we need to follow the same steps as above.

The ‘New-AzSqlDatabase’ command was found in the module ‘Az.Sql’, but the module could not be loaded.

I also got the above error which comes while trying to create an Azure SQL database using PowerShell in Azure.

Below is the cmdlet

# Create a blank DB
$mydatabase = New-AzSqlDatabase  -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -DatabaseName $NewdatabaseName `
    -RequestedServiceObjectiveName "S0" `
The 'New-AzSqlDatabase' command was found in the module 'Az.Sql'

The exact error message was

New-AzSqlDatabase: The ‘New-AzSqlDatabase’ command was found in the module ‘Az.Sql’, but the module could not be loaded. For more information, run ‘Import-Module Az.Sql’.
At line:36 char:15

  • $mydatabase = New-AzSqlDatabase -ResourceGroupName $MyresourceGroupN …
  • ~~~~~
    • CategoryInfo : ObjectNotFound: (New-AzSqlDatabase:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

To fix this error message we need to follow the above same steps.

Before I was getting the error message like below

The 'New-AzSqlServerFirewallRule' command was found in the module 'Az.Sql'

After fixing all these issues by following the above mentioned steps, I didn’t get these errors again

The 'New-AzSqlServerFirewallRule' command was found in the module 'Az.Sql', but the module could not be loaded

Now after the script executed successfully, you can see it created the SQL DB without any issue.

The 'New-AzSqlDatabase' command was found in the module 'Az.Sql', but the module could not be loaded.

You may like following Azure tutorials:

In this Azure tutorial, we discussed how to fix the below errors, 

The ‘New-AzSqlServer’ command was found in the module ‘Az.Sql’, but the module could not be loaded

The ‘New-AzSqlServerFirewallRule’ command was found in the module ‘Az.Sql’, but the module could not be loaded.

The ‘New-AzSqlDatabase’ command was found in the module ‘Az.Sql’, but the module could not be loaded.

I hope it helped you to fix your issue.