I wrote an open source command line utility called SchemaZen that does this. This is much faster than scripts from the management studio, and the output is more version friendly. It supports both schema and data scenarios.
To start the script run:
schemazen.exe script --server localhost --database db --scriptDir c: \ somedir
Then, to recreate the database from the script run:
schemazen.exe create --server localhost --database db --scriptDir c: \ somedir
source share