Advertisements
Home > Powershell, SQL Server > PowerShell for Creating a Database

PowerShell for Creating a Database

This script explains creating a MS SQL Server database with default properties using PowerShell V3.0. In this script we are going to create a database called “PSDB”, by assuming that this database does not yet exist in current instance.

This is equivalent by creating database with following T-SQL Script.

CREATE DATABASE PSDB

Below is PowerShell V3.0 Script for same.

#Import SQL Server Module called SQLPS
Import-Module SQLPS -DisableNameChecking

#Your SQL Server Instance Name
$Inst = "YourInstanceName"
$Srvr = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $Inst

#database PSDB with default settings
#by assuming that this database does not yet exist in current instance
$DBName = "PSDB"
$db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database($Srvr, $DBName)
$db.Create()

#Confirm, list databases in your current instance
$Srvr.Databases |
Select Name, Status, Owner, CreateDate

Hope this helps.

Sandip

Advertisements
  1. February 23, 2013 at 4:11 pm

    lovely stuff post!

  2. March 9, 2013 at 8:36 am

    This is going to be very useful for me thank you very much for posting

  3. vps
    March 14, 2013 at 10:11 pm

    This blog is amazing. I realy like it

  4. July 17, 2013 at 9:12 pm

    Hey There. I discovered your weblog the use of msn.
    This is an extremely neatly written article. I will make sure to bookmark it and come
    back to read more of your helpful information. Thanks for the post.
    I’ll certainly return.

  5. August 19, 2015 at 9:57 pm

    Wonderful blog! Do you have any suggestions for aspioring writers?
    I’m planning to start my owwn webssite soon buut I’m a little lost on everything.

    Would you propose starting with a free platform like WordPress or go for
    a paid option? There are so many options out there that I’m totally confused ..
    Any tips? Kudos!

  6. August 20, 2015 at 1:14 am

    I was recommended this website by mmeans of my cousin.
    I am noww not sure whether this publish is written by him as no one else know such special about my problem.
    You are amazing! Thank you!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: