How to Print a Text File to the Default Printer in Batch Script
Automating the printing of documents, such as daily reports, status logs, or receipts, is a common requirement in many business environments. While you can't easily print complex file types like PDFs or Word documents from a standard batch script, Windows provides a simple, built-in command for sending plain text (.txt, .log, .csv) files directly to a printer queue.
This guide will teach you how to use the classic PRINT command to send a text file to your default printer, how to avoid the interactive prompt that can stall an automated script, and how to select a specific printer other than the default.
The Core Command: PRINT
The PRINT command is a legacy utility that has been part of Windows for a long time. Its function is straightforward: it takes a text file as input and spools it as a print job.
The basic syntax is:
PRINT "filename.txt"
It's important to remember that PRINT is designed for plain text only. Sending a file with complex formatting (like a .docx or .jpg) will result in pages of meaningless characters being printed, as the command does not know how to interpret the file's binary structure.
Basic Example: Printing a Single File
This is the simplest use of the PRINT command.
Let's print a server status log file.
@ECHO OFF
ECHO Sending server_status.log to the default printer...
PRINT "server_status.log"
When you run this command, the file server_status.log is sent to the default printer queue on your Windows machine, and the physical printing process will begin. The command prompt will display a confirmation message.
C:\Scripts\server_status.log is currently being printed
Key PRINT Parameters Explained
The PRINT command has a few switches, but the most important one by far is /D, which allows you to specify a print device.
/D:<Device>: This is the most critical parameter for automation. It specifies the Device (printer) to use. The<Device>is the name of the printer as it appears in Windows (e.g., "HP LaserJet Pro MFP")./C: Cancels a specific print job in the queue./T: Terminates all print jobs in the queue.
Common Pitfalls and How to Solve Them
Problem: The Command Prompts for Confirmation
The first time you run the PRINT command in a new Command Prompt session without specifying a device, it will halt and ask you where to print.
The Error in Action
Name of list device [PRN]:
This interactive prompt will stop your automated script cold, waiting forever for user input.
Solution: Use the /D Switch
The most robust and reliable way to avoid this prompt is to always use the /D switch. Even if you want to print to the default printer, you can specify it using the legacy device name PRN.
@ECHO OFF
REM This command will NEVER prompt.
ECHO Sending file to default printer...
PRINT /D:PRN "server_status.log"
By explicitly telling PRINT where to send the file, you make the command non-interactive and safe for automation.
Problem: Handling Filenames with Spaces
If a filename contains spaces, failing to enclose it in quotes will cause the PRINT command to misinterpret the name and fail.
Solution: Always Quote Your Filenames
This is a fundamental best practice in all batch scripting.
REM This is the correct, safe syntax.
PRINT /D:PRN "My Daily Report.txt"
Problem: Printing to a Specific Printer (Not the Default)
You often need to direct a print job to a specific printer, like a dedicated label printer or a high-volume laser printer in another office.
Solution: Specify the Printer Name with /D
You use the /D switch with the exact name of the printer as it appears in the Windows Control Panel (under "Devices and Printers").
@ECHO OFF
REM The printer's name is "Accounting LaserJet".
SET "PRINTER_NAME=Accounting LaserJet"
SET "FILE_TO_PRINT=invoice_12345.txt"
ECHO Sending invoice to the accounting printer...
PRINT /D:"%PRINTER_NAME%" "%FILE_TO_PRINT%"
This gives you full control over where your documents are printed.
Practical Example: A "Print All" Script for a Folder
This script iterates through all .txt files in a specific "outgoing" folder and prints each one, making it a simple but effective automated printing utility.
@ECHO OFF
SETLOCAL
SET "PRINT_FOLDER=C:\PrintQueue"
ECHO --- Batch Printing Script ---
ECHO.
ECHO Searching for .txt files in "%PRINT_FOLDER%"...
ECHO.
IF NOT EXIST "%PRINT_FOLDER%\*.txt" (
ECHO No .txt files found to print.
GOTO :End
)
FOR %%F IN ("%PRINT_FOLDER%\*.txt") DO (
ECHO Sending "%%~nxF" to the default printer...
REM Use the robust /D:PRN switch to avoid prompting.
PRINT /D:PRN "%%F"
)
ECHO.
ECHO All files have been sent to the printer.
:End
ENDLOCAL
Conclusion
The PRINT command is a simple and effective tool for automating the printing of plain text files from a batch script. While it can't handle complex documents, it is perfect for logs, reports, and other text-based data.
For reliable results in your scripts:
- Always use the
/Dswitch to make the command non-interactive. Use/D:PRNfor the default printer or/D:"Printer Name"for a specific one. - Always enclose filenames in double quotes (
"...") to correctly handle spaces. - Remember that
PRINTis only for plain text files.
By following these guidelines, you can easily integrate automated printing into your Windows batch workflows.