I am sure that this can be achieved by loading the SQL Server Management Objects assemblies but I am curious as to whether the 'Start Powershell' item in 开发者_高级运维the context menu within SQL Server Management Studio 2008 will give me a headstart.
The SMO Server class has a DetachDatabase method that you can use from Start PowerShell. From the server level in sqlps you can use something like this:
PS SQLSERVER:\SQL\Z002>get-childitem | foreach{$_.DetachDatabase("pubs",$false)}
See the MSDN docs for detailed info on DetachDatabase:
精彩评论