Advertisements
Home > Powershell, SQL Server > PowerShell for Droping Database

PowerShell for Droping Database

This article will help you to understand how we can drop existing SQL Server Database with the help of SMO and PowerShell V3.0. If you do not have you sample data base created then follow the steps to create your database with PowerShell V3.0.

#import SQL Server module
Import-Module SQLPS -DisableNameChecking

#your SQL Server Instance Name
$SQLInstanceName = "SQL2012"
$Server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $SQLInstanceName

#provide your database name where you want to change database properties
$DatabaseName = "PSDB"

#create SMO handle to your database
$DBObject = $Server.Databases[$DatabaseName]

#check database exists on server
if ($DBObject)
{
#instead of drop we will use KillDatabase
#KillDatabase drops all active connections before dropping the database.
$Server.KillDatabase($DatabaseName)
}

To drop an SMO server or database object, we can simply invoke the Drop method. But, if you have ever tried dropping a database before, you might have already experienced being blocked by active connections to that database. For this reason, we chose the KillDatabase method, which will kill active connections before dropping the database.

This option is also available in SSMS when you tried to drop a database from Object Explorer. Right-click on a database, the Delete Object window will appear. At the bottom of the window you will find a checkbox called Close existing connections, selection of this check box will fore to kill all active connections before dropping the database.

PowerShell Drop

Hope this helps

Sandip

Advertisements
  1. March 5, 2013 at 10:50 pm

    It’s going to be ending of mine day, however before end I am reading this fantastic piece of writing to increase my experience.

  2. March 6, 2013 at 10:44 am

    I would love to re post this entry on my own website will that be okay

  3. March 10, 2013 at 4:49 am

    As I web site possessor I believe the content matter here is rattling fantastic , appreciate it for your hard work. You should keep it up forever! Good Luck.

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

    This blog is great. I realy like it!

  5. March 18, 2013 at 10:42 pm

    one of our guests lately recommended the following website

  6. April 18, 2013 at 4:50 am

    You are so interesting! I don’t suppose I have read through anything like this before. So good to find somebody with unique thoughts on this issue. Seriously.. thanks for starting this up. This web site is one thing that is needed on the web, someone with a little originality!

  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: