$waUrl = "http://contoso/" $backupDir = "E:\temp\20160126" $dbNamePrefix = "WSS_Content" $files = Get-Item "$($backupDir)\*.bak" foreach($file in $files){ $path = $file.FullName $name = $file.Name $nodt = $name.Substring(12) $pos = $nodt.LastIndexOf('.') $noExt = $nodt.Substring(0, $pos) $srUrl = $noExt.Replace("_","/") $siteUrl = $waUrl.TrimEnd("/") + $srUrl $s = get-spsite -Identity $siteUrl -ErrorAction SilentlyContinue if($s -ne $null){ remove-spsite -Identity $siteUrl -Verbose -Confirm:$false } $dbName = $dbNamePrefix + $noExt $dbExists = Get-SPContentDatabase -Identity $dbName -ErrorAction SilentlyContinue if($dbExists -ne $null){ Remove-SPContentDatabase -Identity $dbName -Force -Verbose -Confirm:$false } New-SPContentDatabase -Name $dbName -WebApplication $waUrl -Verbose -Confirm:$false Restore-SPSite -Identity $siteUrl -ContentDatabase $dbName -Path $path -Verbose -Confirm:$false }