--- title: "PowerShell: Add a database to an auto-failover group" description: Use an Azure PowerShell example script to create a database in Azure SQL Database, add it to an auto-failover group, and test failover. author: AbdullahMSFT ms.author: amamun ms.reviewer: wiassaf, mathoma ms.date: 07/16/2019 ms.service: sql-database ms.subservice: high-availability ms.topic: sample ms.custom: - "sqldbrb=1" - "devx-track-azurepowershell" ms.devlang: PowerShell --- # Use PowerShell to add a database to a failover group [!INCLUDE[appliesto-sqldb](../../includes/appliesto-sqldb.md)] This PowerShell script example creates a single database in Azure SQL Database, creates a failover group, adds the database to it, and tests failover. [!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)] [!INCLUDE [updated-for-az](../../includes/updated-for-az.md)] [!INCLUDE [cloud-shell-try-it.md](../../includes/cloud-shell-try-it.md)] If you choose to install and use PowerShell locally, this tutorial requires Az PowerShell 1.4.0 or later. If you need to upgrade, see [Install Azure PowerShell module](/powershell/azure/install-az-ps). If you are running PowerShell locally, you also need to run `Connect-AzAccount` to create a connection with Azure. ## Sample scripts [!code-powershell-interactive[main](~/../powershell_scripts/sql-database/failover-groups/add-single-db-to-failover-group-az-ps.ps1 "Add a database to a failover group")] ## Clean up deployment Use the following command to remove the resource group and all resources associated with it. ```powershell Remove-AzResourceGroup -ResourceGroupName $resourceGroupName ``` ## Script explanation This script uses the following commands. Each command in the table links to command specific documentation. | Command | Notes | |---|---| | [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup) | Creates a resource group in which all resources are stored. | | [New-AzSqlServer](/powershell/module/az.sql/new-azsqlserver) | Creates a server. | | [New-AzSqlServerFirewallRule](/powershell/module/az.sql/new-azsqlserverfirewallrule) | Creates a server-level firewall rule for a server. | | [New-AzSqlDatabase](/powershell/module/az.sql/new-azsqldatabase) | Creates a new database. | | [New-AzSqlDatabaseFailoverGroup](/powershell/module/az.sql/new-azsqldatabasefailovergroup) | Creates a new failover group. | | [Get-AzSqlDatabase](/powershell/module/az.sql/get-azsqldatabase) | Gets one or more databases. | | [Add-AzSqlDatabaseToFailoverGroup](/powershell/module/az.sql/add-azsqldatabasetofailovergroup) | Adds one or more databases to a failover group. | | [Get-AzSqlDatabaseFailoverGroup](/powershell/module/az.sql/get-azsqldatabasefailovergroup) | Gets or lists failover groups. | | [Switch-AzSqlDatabaseFailoverGroup](/powershell/module/az.sql/switch-azsqldatabasefailovergroup)| Executes a failover of a failover group. | | [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup) | Removes a resource group | ## Next steps For more information on Azure PowerShell, see [Azure PowerShell documentation](/powershell/azure/). Additional SQL Database PowerShell script samples can be found in the [Azure SQL Database PowerShell scripts](../powershell-script-content-guide.md).