Microsoft Windows: Create a Batch File from the Command Line

Here’s how to create any text or batch file from the command line in Windows. Works in all versions of Windows 3.11 and higher, including Windows 8 consumer preview.

Sometimes you need to create a text or batch file from the command line in Windows. It’s easy to do that — and make sure it’s got the right extension, too. Here’s how.

This tip will work in any version of Windows, even Windows 8 Consumer Preview.

Here’s an easy way to make sure that, when you do create it, it’s got the correct extension.

Click Start>Run>and type: CMD
Hit Enter.

From the command prompt, type:

echo enteryourtexthereaswhateveryouwantfiletohaveascontents > filename.fileextension

Or create a file that will run the defragmentation engine in Windows as a batch file. To do that, type:

echo defrag c: > defragc.bat

Hit Enter.

This will put your batch file in the folder where the command line resides — in this case, my Desktop. Note I don’t get a confirmation when it completes successfully.