Skip to content

Instantly share code, notes, and snippets.

@develohpanda
Last active May 24, 2022 15:38
Show Gist options
  • Save develohpanda/255292ffa58a0924b20662442c365e24 to your computer and use it in GitHub Desktop.
Save develohpanda/255292ffa58a0924b20662442c365e24 to your computer and use it in GitHub Desktop.
Drop SQL database from Powershell
function Delete-SqlDatabase($serverName, $databaseName) {
Import-Module SQLPS
$server = New-Object Microsoft.SqlServer.Management.Smo.Server($serverName)
$db = $server.databases[$databaseName]
if ($db) {
$server.KillAllprocesses($databaseName)
$db.Drop()
}
}
@rajeshsys
Copy link

Hi,
This worked perfectly for the first time and then it gave all sorts of error for Import-Module SQLPS/SQLServer. any one faced similar issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment