Problem
I have seen some of your SQL Server Backup tips. I am starting to learn PowerShell and want to see how I can use it for common DBA tasks. How can I create a basic PowerShell script to backup all my user databases? Check out this tip to learn more.
Solution
With T-SQL, a CURSOR is used to build the BACKUP command and execute it for each database. You can see an excellent example of that process with this tip (Simple script to backup all SQL Server databases). In PowerShell you will use a variable that is populated with all your database names on the instance, and pass each one of those to a ForEach loop. The ForEach loop takes the place of using a CURSOR. The following script will allow you to backup each user database within a single instance. The format of the backup file will be “DBName_YearDayMonth.bak”. In this example the directory is set to “C:\TEMP”, you will need to change this to your desired location.
#load SQL snap-in
Add-PSSnapin *SQL*#pull the current date
$date = Get-Date -Format yyyyddMM#set location of backup files
$directory = “C:\temp\”#Grab the database names
$dbname = dir ‘SQLSERVER:\SQL\KRINGER\DEFAULT\Databases’ | Select Name#Backup each user database found.
$dbname | foreach { $_.Name.ToString() } | foreach {$bakfile = “$directory” + $_ + “_” + $date + “.bak”;
“Backing up Database: $_”; Invoke-Sqlcmd -SuppressProviderContextWarning -Query
“BACKUP DATABASE $_ TO DISK=N’$bakfile’ WITH INIT”;
}
Your output should look similar to this after you have executed the script:

The “-SuppressProviderContextWarning” is used to suppress a warning message that shows up each time you run the Invoke-Sqlcmd cmdlet. You can also use “-IgnoreProviderContext” as well. Also, the “$_.Name.ToString()” is for formatting the database name so it agrees with the BACKUP command. If you leave that out you will see what I am talking about. The object type of the database name does not agree with the BACKUP command and this is the method I came up with for getting it to work.
The script takes you through the following steps:
- Load the SQL Server Snap-ins.
- In order to add the date to our backup file name we first have to grab it in the format we want and store it in a variable. Using the “-Format” allows us to specifically get it as YearDayMonth.
- Set our directory path for the location of the backup files.
- Pull in our names of the user databases. SQL Server PowerShell hides the system databases so this command will only return our user database. If you want to include the system databaes in the backup, just add “-Force” after the path.
- Finally we pass each database name through the pipeline to our ForEach loop. I added in the “Backing up Database” part as an indicator. It will show the current DB name that is getting backed up as it goes through them.
Next Steps
- Check out other great tips on SQL Server & PowerShell.
- Check out MSDN and TechNet to get more familar with SQL Server PowerShell.
- To better understand SQL Server Backups read through this TechNet Magazine article by Paul Randal: Understanding SQL Server Backups.
- Check out these SQL Server Backup resources on MSSQLTips:

Biography
Shawn Melton started his IT career in 2002 after graduating college. He started working with SQL Server in 2005 and has worked with SQL Server 2000, 2005, 2008, and 2008 R2. He has been working as a full time DBA since 2009 in Montgomery, AL.
Community Activity
Shawn is a frequent contributor on Ask SSC and Stack Exchange forums helping out where he can with SQL Server and PowerShell questions.
SQL Server Interests
Shawn enjoys just working with SQL Server and sharing what he has learned with the community through blogging and contributing on forums. He has become interested in PowerShell as well and how it can be used both with SQL Server and other administrative tasks.
Publications
He makes contributions on MSSQLTips.com, but does have one article published on SQL Server Central as well (and hopes to increase that over time).
Social Media
Contact Information
Shawn can be reached at wshawnmelton (AT) gmail DOT com.