This script creates a file and fills it with the content of the @Text string.
You need to ensure OLE Automation is enabled for this to work (command to turn OLE Automation is at the top of the script).
Ensure that the SQL Server Service user is granted rights to create files and write in the directory where you wish to output the file.
Set @File = full path of output file
Set @Text = desired content for the file.
Note: if you use
EXECUTE sp_OAMethod @FileID,'WriteLine',NULL,@Text
in a loop you can add as many lines to the file as you need to add (just keep updating @Text to the content for the next line)