How to delete orphaned site collections with powershell

On one of my customers, I had to upgrade one Nintex Forms from and old version to the most recent one, when doing the upgrade I had these nice error on the Nintex Wizard.

Cannot find side with id '*'.

After several hours of useless support with Nintex, where they were not able to help, we encountered this easy way to delete the orphaned sites. After this script is run, the Nintex Upgrade worked fine.

#remove any sitecollection that is not found in the sitemap
$database = Get-SPContentDatabase
#or just $database = @{name="MyContentDatabaseName"}
Foreach ($DBname in ${  
    #"stsadm -o enumallwebs" gives back an XML which we can parse for the orphaned sites
    [xml]$allwebs = stsadm -o enumallwebs -databasename $DBname 
    #filter those that are missing in the sitemap and pass them back to stsadm to delete
    $OrphanedSites = $ | ?{$_.insitemap -match "false"}
    Foreach($site in $OrphanedSites){
         Write-Host $site.Name
         Stsadm -o deletesite -force -siteid  $site.Id -databasename $DBname -databaseserver $allwebs.Databases.Database.datasource