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 of this task is as follows:
CREATE DATABASE TestDB
How to do it...
Follow these steps to create a simple database in SQL Server:
Open PowerShell ISE as administrator.
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 = "localhost" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run:
#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()...