Drop SQL Server database from powershell
I found a different command to do this. It was simply:
invoke-sqlcmd -ServerInstance ".\SC" -U "sa" -P "MyPlainTextPass" -Query "Drop database MyDatabase;"
The only way to make it work for me was to force any other connections to the database to close, through the following command:
Invoke-SqlCmd -ServerInstance $Server @Auth `
-Query "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='MyDBName') `
BEGIN `
ALTER DATABASE [MyDBName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; `
DROP DATABASE [MyDBName]; `
END;" `
-Verbose
The backticks (`) are necessary, otherwise this command would have to be inlined