Skip to content
Dec 6 2008

Creating a SQL Server Database from PowerShell

My PowerShell scripting madness continues, with the need to automate the setup and teardown of a SQL Server database in prep for running automated functional tests. Here’s what I came up with:

# Get the parameters passed to the script
Param($dbInstance, $dbName)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$dbServer = new-object Microsoft.SqlServer.Management.Smo.Server ($dbInstance)
$db = new-object Microsoft.SqlServer.Management.Smo.Database
# Loop thru the db list to find the one we need. If found, set the local
# vars to avoid errors when trying to delete the db from within the loop.
$found = "false"
foreach ($_ in $dbServer.Databases)
{
if ($_.Name -eq $dbName)
{
$db = $_
$found = "true"
}
}
# Now that we're out of the loop we can kill the db
if ($found -eq "true")
{
"Deleting database $dbName..."
$dbServer.KillAllProcesses($db.Name)
$dbServer.KillDatabase($db.Name)
}
"Creating database $dbName..."
$db = new-object Microsoft.SqlServer.Management.Smo.Database ($dbServer, $dbName)
$db.Create()

So to use this, let’s say this is in a script named C:\CreateDatabase.ps1 and you want to create a database named MyDatabase on the default instance of the local SQL Server. This is what it would look like:

PowerShell-CreateDatabase

The -dbInstance and -dbName are named parameters passed to the script, which are grabbed in the first line of the script. PowerShell didn’t like “(local)” passed in, so I had to escape the double-quotes to get it to be recognized properly, although you could certainly use the server name instead; I just wanted to show using “(local)” as an example. But other than that, all works as expected without any issues.

There’s several other properties you can set on the database when creating it, such as the size, growth type, and file name, but if all you need are the defaults, the above script will take care of it.

Similar Posts:

  • http://dougfinke.com/blog/?cat=3 Doug

    Nice post.

    Is it much more work to point the new database to a different drive?

  • http://code-inside.de/blog/2011/01/28/best-practice-versionskontrolle-datenbanken-und-db-updates-wie-teile-ich-die-db-im-team/ Best Practice: Versionskontrolle, Datenbanken und DB-Updates… wie teile ich die DB im Team? | Code-Inside Blog

    [...] an. Wenn es die DB vorher nicht gab, legt er sie ebenfalls an. Das Script habe ich von Dave Donaldsons Blog. Resultat davon: Ein sauberer [...]

  • http://code-inside.de/blog-in/2011/03/06/best-practice-version-check-data-base-and-db-updates-how-to-split-the-db-in-the-team/ Best Practice: version check, data base and DB updates… how to split the db in the team? | Code-Inside Blog International

    [...] The script looks up if there is another file base named “BusinessBingo”. If there is one he will delete the old and creates the new one with the same name. If there wasn’t one before he is going to save this one. This script is from the blog of Dave Donaldson. [...]