Creating a database
This recipe walks through creating a database with default properties using PowerShell.
Getting ready
In this example, we are going to
create a database called TestDB
, and we assume that this database does not yet exist in your instance.
For your reference, the equivalent T-SQL code for this task is:
CREATE DATABASE TestDB
How to do it...
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Import the
SQLPS
module, and create a new SMO Server object:#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "KERRIGAN" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run it:
#database TestDB with default settings #assumption is that this database does not yet exist $dbName = "TestDB" $db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database($server, $dbName) $db.Create...