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.

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


  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!

  7. sandy
    October 16, 2017 at 4:02 am

    I used the script first time successfully but after that it just gives me error whilst creating $DBObject.

    Cannot index into a null array.
    At C:\work\PowerShell\dropDatabase.ps1:34 char:1
    + $DBObject = $Server.Databases[$DatabaseName]
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

  8. sandy
    October 16, 2017 at 4:19 am

    Sorry ignore above cause I have inserted another $server as string and that’s why it was returning null…cheers for the scripts.

  9. mani
    January 24, 2022 at 7:59 pm

    the goal is to delete the DB, dropping is not the cases. I want to delete the DB but it isn’t working. can someone be nice and tell me how to delete the DB using above mention script. because for me it is a failed attempt.

  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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: