PowerShell is a cross-SQL Server PowerShell is a platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework. It includes two primary modules: SQLPS and SQL Server PowerShell. SQLPS is the command-line shell and scripting language, providing a way to automate tasks and interact with SQL Server.
SqlServer provides various cmdlets for different tasks, including backing up SQL Server database objects. The Backup-SqlDatabase cmdlet can be used for this purpose, and it offers several commonly-used parameters.
You can use the SQL Server cmdlets in PowerShell to backup multiple SQL databases. The Backup-SqlDatabase cmdlet allows you to specify a database to backup, while the Backup-SqlDatabase -BackupFile cmdlet enables you to specify a file path for the backup.
The SqlServer module was previously included with SQL Server Management Studio (SSMS), but only in versions 16.x. To use PowerShell with SSMS 17.0 and later, you need to install the SqlServer module from the PowerShell Gallery.
After installing the SqlServer module, you can write a PowerShell script to backup SQL Server databases by importing the module, getting the list of databases on the SQL Server instance using `Get-SqlDatabase`, and then backing up each database using `Backup-SqlDatabase` with the desired parameters. You can also specify the backup file location for all databases using the `-BackupFile` parameter.
1. To start Windows PowerShell ISE, click the Start menu, select Windows PowerShell, and then click Windows PowerShell ISE.
Note: The Windows PowerShell Integrated Scripting Environment (ISE) is a graphical user interface that allows you to run commands, write, test, and debug scripts all within a single Windows-based environment.
2. In Windows PowerShell ISE, click "New Script" on the toolbar and type the following cmdlets to backup multiple SQL databases. For example, to backup the "TestDB" and "DemoDB" databases, you would enter the necessary commands.
✍ Tips:
① The cmdlets create a complete database backup for "TestDB" and "DemoDB" and save the backup files to the default backup location on server "DESKTOP-J6AHKLA", which is C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup. The server name specifies the default instance, and you can use -ServerInstance "Computer\Instance" to specify a different instance. The -Initialize parameter overwrites any existing backup sets on the media, making this backup the first set, and omitting it would result in large ".bak" files with multiple database copies after running the script multiple times.
3. To backup SQL Server databases for the first time, click on the "Run Script" button on the toolbar. This will initiate the backup process, and you can monitor its progress and outcome in the Script Pane.
4. To save a script in Windows PowerShell ISE, click on the "File" menu, select "Save As," choose a location and name for the file, and then click "Save."
Method 1: Run the ".ps1" file with PowerShell
Method 2: Run script from Windows PowerShell ISE
The steps are the same as mentioned above. Just use the cmdlet below:
Get-ChildItem "SQLSERVER:\SQL\DESKTOP-J6AHKLA\DEFAULT\Databases" | Backup-SqlDatabase
✍ Tips:
◑ The command backs up all user-created databases on the server instance "DESKTOP-J6AHKLA\DEFAULT" to the default backup location, creating a backup file for each database named [database name].bak.
◑ System databases are not backed up by default, but you can use the Backup-SqlDatabase cmdlet to specifically back up the system databases "master", "model", and "msdb". This allows you to manually create backups of these critical system databases.
You can use the Backup-SqlDatabase cmdlet repetitively to backup several databases, but for a large number of databases, PowerShell SQL backup is recommended. To automate the process for multiple databases, you can use a SQL Server Management Studio (SSMS) tool that allows you to select and backup multiple databases at once, eliminating the need to run the cmdlet repetitively. This approach streamlines the backup process for a large number of databases.
Qiling Backup, a powerful SQL backup software, is here to help. With its features, you can easily perform SQL Server backup, taking advantage of its capabilities to ensure the security and reliability of your database.
★ The SQL Server Backup feature allows you to backup multiple instances and databases on PCs/Servers, and it also offers a schedule feature to automate database backups, enabling you to perform backups daily, weekly, or monthly.
★ The software supports backing up databases on all popular SQL Server versions, including SQL Server 2022, 2019, 2017, 2016, 2014, 2012, 2008 (R2), and 2005.
Download the free trial of Qiling Backup and explore all the wonderful features right now!
I've scheduled a data backup for the added SQL Server. The backup will run automatically at the specified time.
✍More useful features:
To backup multiple SQL databases, you can write a PowerShell script. However, this method is only suitable for a small number of databases. For a large number of databases, a more efficient solution is to use Qiling Backup, which can automate the backup process for you.