PowerShell Quick Script – Delete all databases from list of instances

I have been using this amazing PowerShell module called dbatools (can be downloaded from https://dbatools.io/) for a while. This is basically a module that has bunch of very very handy commands that can be used to do lot of SQL Server administrative operations very easily… for example, migrate database to another instance\server, restore collection of backups, verify backups … some really crazy administrative tasks . And if you have dealt with any of them, you know how hard it is to actually do them if you do it via GUI (aka SSMS).

Recently I ran into situation where I had to remove all databases for bunch of instances and this module saved me great deal of time. So I uploaded that script to GitHub. Script itself is very simple…

 

Clear-Host
Import-Module -Name "C:\Program Files\WindowsPowerShell\Modules\dbatools"
$username = "sa"
$password = ConvertTo-SecureString -String "MyPa$$W0rd" -AsPlainText -Force
$dbcred = New-Object System.Management.Automation.PSCredential $Username,$Password
# List of instance names for which ALL databases need to be removed
$instanceName=@("DEVUS3042452-01", "DEVUS3042452-02", "TEST34508-23","AZLKJE234082")
Get-DbaDatabase -SqlInstance $instanceName -SqlCredential $dbcred -ExcludeSystem | Remove-DbaDatabase

Script takes credentials for SA and feeds it to list of instances and then it removes all databases from those instances except system databases. Script itself is for very specific purpose hence it assumes common SQL Login that is present in all instances… very lazy script if you ask me :D.

But I uploaded it to GitHub not that because this script is very genius… it is not. In fact it is very very basic script… but mainly because I wanted to get my feet wet with this another tool\technology that I have been avoiding in better part of my IT carrier.. GIT. I find SVN bit more user friendly even though bit limited than GIT. But I guess this is something I can’t avoid anymore if I don’t want to sound too old :D.

Anyhow… that’s it for now…

Leave a Reply

Your email address will not be published.