If you want detach script, run the above script with @DetachOrAttach = 1. Run with 0 for attach script.
Script will print detach script or attach script for all user databases. You can compy the script from the "messages" screen in ssms and execute to detach/attach. If you want to detach or attach specific databases you can modify the cursor sql accordingly.