How to create an Azure SQL server using PowerShell

Create an Azure SQL Server

  1. Install the latest version of PowerShell.
  • PowerShell 7.0.6 LTS, PowerShell 7.1.3, or higher is recommended by Microsoft.
  • We can check the PowerShell version with the following command.
$PSVersionTable.PSVersion

We can use the below terminal to run the PowerShell commands.

Note: It takes a minute to start and install necessary things when we connect to the terminal for the first time. We’ll have to wait till it initializes PowerShell.

Terminal 1
Terminal
Loading...
  1. Set the Execution Policy. In a Windows-based system, it is necessary to set. In UNIX-based systems, it may not work, nor is it required.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

We can check the execution policy with the following command.

Get-ExecutionPolicy -List
  1. Install the Az module.
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
  1. Login into your account with the following command.

It will provide a link that can be opened in the browser, where we enter the code that has been provided.

Connect-AzAccount -UseDeviceAuthentication

After successfully logging in, it will print our Account, SubscriptionName, TenantId, and Environment.

We’ve set up everything that’s needed to create an Azure SQL Server using PowerShell.

  1. Create an Azure SQL server using the following command.
New-AzSqlServer -ResourceGroupName "demoRG" -Location "Central US" -ServerName "demosvr" -ServerVersion "12.0" -SqlAdministratorCredentials (Get-Credential)

In the above command, we see the following:

  • Cmdlet New-AzSqlServer creates an Azure SQL server.
  • demoRG is the resource group name where our SQL server resource will reside. demoRG must be created before executing this command.
  • SQL server will be created at the location Central US.
  • Provide the server name as demosvr.
  • Provide the SQL Server version.
  • Once the command is executed, (Get-Credential asks for credentials.

It will take some time to create a SQL server and print the details when it is created.

Free Resources