Scripting out Stored Procedures and Functions using Query

One of my friends asked me if there is an way to script out the definitions of stored Procedures and Functions using Query. There can be several methods. This is one of the methods that uses BCP utility

All you have to do is replace DBname by the actual Database Name

EXEC master..xp_cmdshell 
'bcp "Select routine_definition from 
DBname.information_Schema.routines 
order by routine_name" queryout "C:\scripts.sql" -c'

After it runs successfully, the file C:\scripts.sql will have the scripts

Note that due to security issues, xp_cmdshell is disable by default. You need to use sp_configure to enable it. You can find more information in this https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/xp-cmdshell-server-configuration-option