Automatically moving SharePoint site collections to separate databases

I had a requirement to move site collections into separate databases after a large migration. For this to work, I have one content database that is created as default with maximum number of sites set to 5000. My script will then create new databases with a site limit of 1 and move the site collections to that database.

#Declare params

$sourceDB = 'AutoSPInstaller_Content_Portal'
#NOTE: DATABASE SERVER NAME MUST BE EXACTLY AS ENTERED IN CENTRAL ADMIN! SEE PREVIOUS CONTENT DB FOR CORRECT NAME!
$dbserver = 'lekman-sql'
$webapp = 'http://portal.lekman.com'
$prefix = 'SP_Content_'
#Main logic

$sites = Get-SPSite -Limit All -ContentDatabase $sourceDB
Write
-Host -ForegroundColor Yellow 'The following sites are within the source DB'
$sites | select url, @{label="Size";Expression={$_.usage.storage}}
Write
-Host
Foreach ($site in $sites) {

$regex = new-object System.Text.RegularExpressions.Regex ('[^a-zA-Z0-9]', [System.Text.RegularExpressions.RegexOptions]::MultiLine)
$uri = New-Object System.Uri($site.Url)
$dbname = $regex.Replace($site.Url.Replace($uri.Host + '/', '').Replace('http://','').replace('https://',''), '-')
$dbname = $prefix + $dbname
Write
-Host 'Moving site '$site.Url
#Create content db
Write-Host ? Creating database?$dbname
$db = New-SPContentDatabase -Name $dbname -DatabaseServer $dbserver -WebApplication $webapp -MaxSiteCount 1 -WarningSiteCount 0
Write
-Host " Moving site..."
Move
-SPSite $site.Url -DestinationDatabase $dbname -Confirm:$false
Write
-Host
}
Write
-Host -ForegroundColor Yellow 'You must now perform an IIS reset to make changes take effect.'
pause
iisreset
Write
-Host -ForegroundColor Yellow 'Database migration completed'

Note: You should not be running this at the same time as any migrations or site collection creations. It will also require an IISRESET so plan this properly.

Hope this helps anyone else.