Skip to content

Instantly share code, notes, and snippets.

@develohpanda
Last active July 2, 2024 19:05
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()
}
}
@gwInsurtech24
Copy link

Awesome, thanks!

@FrancoisCamus
Copy link

Thanks for this!

@mikhail-cabilao
Copy link

Thanks!

@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