You can use the below script to do the same. I've excluded system db's since the same can't be removed.
set nocount on
declare @dbname as varchar(80)
declare @server_name as varchar(20)
select @server_name = @@servername
declare rs_cursor CURSOR for select name from master.dbo.sysdatabases where name not in ('model','master','msdb','tempdb','alert_db','mssecurity')
open rs_cursor
Fetch next from rs_cursor into @dbname
IF @@FETCH_STATUS <> 0
PRINT 'No database to backup...Please check your script!!!'
WHILE @@FETCH_STATUS = 0
BEGIN
print 'sp_detach_db ' + @dbname
print 'go'
print 'print ''Detach of ' + upper(@dbname) + ' database successfully completed'''
print 'go'
PRINT ' '
FETCH NEXT FROM rs_cursor INTO @dbname
END
CLOSE rs_cursor
deallocate rs_cursor
print ' '
print 'print ''SERVER NAME : ' + upper(@server_name) + '--> All databases successfully detached'''
Copy the above script and execute it, you will get the output as detach script . Copy the output and execut it. All the user db's will be detached!!!!
If you face any problem let me know
Regards..Vidhya Sagar
SQL-Articles