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.
Hope this helps
Sandip
It’s going to be ending of mine day, however before end I am reading this fantastic piece of writing to increase my experience.
I would love to re post this entry on my own website will that be okay
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.
This blog is great. I realy like it!
one of our guests lately recommended the following website
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!
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
Sorry ignore above cause I have inserted another $server as string and that’s why it was returning null…cheers for the scripts.
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.