How to create an Azure SQL server using PowerShell
Create an Azure SQL Server
- Install the latest version of PowerShell.
- PowerShell
7.0.6 LTS, PowerShell7.1.3, orhigheris 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.
- 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
- Install the
Azmodule.
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
- 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.
- 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-AzSqlServercreates an Azure SQL server. demoRGis the resource group name where our SQL server resource will reside.demoRGmust 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-Credentialasks for credentials.
It will take some time to create a SQL server and print the details when it is created.