Azure PowerShell samples for Azure SQL Database
Azure SQL Database enables you to configure your databases, instances, and pools using Azure PowerShell.
Open Azure Cloud Shell
Azure Cloud Shell is an interactive shell environment hosted in Azure and used through your browse. Azure Cloud Shell allows you to
PowerShell shells to run a variety of tools to work with Azure services. Azure Cloud Shell comes pre-installed with the commands
to allow you to run the content of this article without having to install anything on your local environment.
To run any code contained in this article on Azure Cloud Shell, open a Cloud Shell session, use the Copy button on a code block to copy the code, and paste it into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS. Pasted text is not automatically executed, so press Enter to run code.
You can launch Azure Cloud Shell with:
|Select Try It in the upper-right corner of a code block. This doesn't automatically copy text to Cloud Shell.|
|Open Azure Cloud Shell in your browser.|
|Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal.|
If you choose to install and use the PowerShell locally, this tutorial requires AZ PowerShell 1.4.0 or later. If you need to upgrade, see Install Azure PowerShell module. If you are running PowerShell locally, you also need to run
Connect-AzAccount to create a connection with Azure.
Single Database and Elastic pools
The following table includes links to sample Azure PowerShell scripts for Azure SQL Database.
|Create and configure single databases, and elastic pools|
|Create a single database and configure a database server firewall rule||This PowerShell script creates a single Azure SQL database and configures a server-level firewall rule.|
|Create elastic pools and move pooled databases||This PowerShell script creates Azure SQL Database elastic pools, and moves pooled databases, and changes compute sizes.|
|Configure geo-replication and failover|
|Configure and failover a single database using active geo-replication||This PowerShell script configures active geo-replication for a single Azure SQL database and fails it over to the secondary replica.|
|Configure and failover a pooled database using active geo-replication||This PowerShell script configures active geo-replication for an Azure SQL database in a SQL elastic pool, and fails it over to the secondary replica.|
|Configure and failover a failover group for a single database||This PowerShell script configures a failover group for an Azure SQL Database server instance, adds a database to the failover group, and fails it over to the secondary server|
|Scale a single database and an elastic pool|
|Scale a single database||This PowerShell script monitors the performance metrics of an Azure SQL database, scales it to a higher compute size and creates an alert rule on one of the performance metrics.|
|Scale an elastic pool||This PowerShell script monitors the performance metrics of an Azure SQL Database elastic pool, scales it to a higher compute size, and creates an alert rule on one of the performance metrics.|
|Auditing and threat detection|
|Configure auditing and threat-detection||This PowerShell script configures auditing and threat detection policies for an Azure SQL database.|
|Restore, copy, and import a database|
|Restore a database||This PowerShell script restores an Azure SQL database from a geo-redundant backup and restores a deleted Azure SQL database to the latest backup.|
|Copy a database to new server||This PowerShell script creates a copy of an existing Azure SQL database in a new Azure SQL server.|
|Import a database from a bacpac file||This PowerShell script imports a database to an Azure SQL server from a bacpac file.|
|Sync data between databases|
|Sync data between SQL databases||This PowerShell script configures Data Sync to sync between multiple Azure SQL databases.|
|Sync data between SQL Database and SQL Server on-premises||This PowerShell script configures Data Sync to sync between an Azure SQL database and a SQL Server on-premises database.|
|Update the SQL Data Sync sync schema||This PowerShell script adds or removes items from the Data Sync sync schema.|
Learn more about the Single Database Azure PowerShell API.
The following table includes links to sample Azure PowerShell scripts for Azure SQL Database - Managed Instance.
|Create and configure managed instances|
|Create and manage a Managed Instance||This PowerShell script shows you how to create and manage a Managed Instance using the Azure PowerShell|
|Create and manage a Managed Instance using Azure Resource Manager template||This PowerShell script shows you how to create and manage a Managed Instance using the Azure PowerShell and Azure Resource Manager template.|
|Configure Transparent Data Encryption (TDE)|
|Manage Transparent Data Encryption in a Managed Instance using your own key from Azure Key Vault||This PowerShell script configures Transparent Data Encryption (TDE) in Bring Your Own Key scenario for Azure SQL Managed Instance, using a key from Azure Key Vault|
Learn more about the Managed Instance Azure PowerShell API.
The examples listed on this page use the Azure SQL Database cmdlets for creating and managing Azure SQL resources. Additional cmdlets for running queries, and performing many database tasks are located in the sqlserver module. For more information, see SQL Server PowerShell.
Send feedback about: