How about this? Less lines
select 'ALTER DATABASE [' + name + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;' + char(10)
+ 'EXEC master.dbo.sp_detach_db @dbname = N''' + name + ''';' + CHAR(10)
from sys.databases Where database_id > 5 and name not in ('distribution') order by name