How to Print the Contents of the Screen in Batch Script
In a command-line environment, "printing the screen" doesn't mean taking a graphical screenshot (like pressing the PrtScn key). Instead, it refers to capturing the text output that has been displayed in the console window and sending it to a physical printer. This is a useful feature for creating a hard copy of a diagnostic report, a log file's contents, or the results of a command for review.
This guide will explain the two-step process required to do this, as batch scripting has no direct command to print the screen buffer. You will learn how to first redirect the output of your commands to a temporary file, and then use the built-in PRINT command to send that file to your printer.
The Challenge: No Direct "Print Screen" Command
The cmd.exe console does not have a built-in command to capture the text that is currently visible in its window buffer. The text you see is the result of the Standard Output streams of the commands you've run. Therefore, to "print the screen," you must proactively capture these output streams as they are generated.
The Core Method: Redirect to a File, Then Print
The standard and only reliable method involves two distinct steps:
- Redirect Output: Run the command(s) whose output you want to print, but use the redirection operator (
>) to save that output to a temporary text file instead of displaying it on the screen. - Print the File: Use the
PRINTcommand to send the temporary text file you just created to the default printer. - (Optional but Recommended) Clean up by deleting the temporary file.
Basic Example: A Simple "Print DIR Output" Script
This script will run the DIR command, capture its output, print it, and then clean up.
@ECHO OFF
SET "TempFile=%TEMP%\screen_contents.txt"
ECHO --- Printing the output of the DIR command ---
ECHO.
ECHO Step 1: Capturing the command output to a temporary file...
DIR > "%TempFile%"
ECHO Step 2: Sending the temporary file to the default printer...
REM Use /D:PRN to avoid any confirmation prompts.
PRINT /D:PRN "%TempFile%"
ECHO Step 3: Cleaning up the temporary file...
DEL "%TempFile%"
ECHO.
ECHO --- Print job has been sent ---
How the Two-Step Process Works
DIR > "%TempFile%": This command executesDIR. Instead of showing the directory listing in the console, the>operator redirects the entire Standard Output stream and writes it to the filescreen_contents.txtin your temporary folder.PRINT /D:PRN "%TempFile%": This command takes the text file we just created and spools it as a print job to the default system printer./D:PRNis a switch that explicitly specifies the default print device, which makes the command non-interactive.DEL "%TempFile%": This is good housekeeping. It removes the temporary file so your temp folder doesn't get cluttered.
What About a Graphical Screenshot? (The PowerShell Method)
If your goal is to take an actual screenshot (an image of the screen), batch is not the right tool. However, a batch script can easily call a PowerShell one-liner to accomplish this.
For example, a script to take a screenshot:
@ECHO OFF
SET "ScreenshotPath=%USERPROFILE%\Desktop\screenshot.png"
ECHO Taking a screenshot and saving it to the desktop...
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('{PRTSC}'); Start-Sleep -m 200; $img = [System.Windows.Forms.Clipboard]::GetImage(); $img.Save('%ScreenshotPath%', [System.Drawing.Imaging.ImageFormat]::Png)"
ECHO Screenshot saved.
This is an advanced command, but it is the correct way to get a graphical capture from a script.
Common Pitfalls and How to Solve Them
Problem: The PRINT Command Prompts for Input
If you use the PRINT command without any switches, it may halt your script and ask you to confirm the printer (Name of list device [PRN]:).
Solution: Always use the /D:PRN switch. This explicitly tells the PRINT command to use the default printer and makes it run non-interactively, which is essential for automation.
Problem: Forgetting to Clean Up the Temporary File
If your script runs frequently, you will leave behind many temporary files, which wastes disk space.
Solution: Always include a DEL command at the end of your script to remove the temporary file you created.
Practical Example: A "Print System Health" Script
This script gathers several key pieces of diagnostic information, saves them all to a single report file, and then prints that report for a hard-copy record.
@ECHO OFF
SETLOCAL
TITLE System Health Report Printer
SET "ReportFile=%TEMP%\System_Health_Report.txt"
ECHO --- System Health Report Generator ---
ECHO.
ECHO This script will gather system information and print it.
PAUSE
ECHO Step 1: Generating the report...
REM --- Use a parenthesized block to redirect all output at once ---
(
ECHO System Health Report for: %COMPUTERNAME%
ECHO Generated on: %DATE% at %TIME%
ECHO ==========================================================
ECHO.
ECHO --- IP CONFIGURATION ---
ipconfig /all
ECHO.
ECHO ==========================================================
ECHO.
ECHO --- DISK SPACE ---
fsutil volume diskfree C:
ECHO.
) > "%ReportFile%"
ECHO Step 2: Sending report to the printer...
PRINT /D:PRN "%ReportFile%"
ECHO Step 3: Cleaning up...
DEL "%ReportFile%"
ECHO.
ECHO [SUCCESS] The report has been sent to the default printer.
ENDLOCAL
Conclusion
While a batch script cannot directly print the contents of the console buffer, it provides a simple and powerful two-step mechanism to achieve the same result.
The key process is:
- Redirect: Run your desired command(s) and use the
>or>>operator to capture their text output into a temporary file. - Print: Use the
PRINT /D:PRN "tempfile.txt"command to send that file to the printer.
This method gives you complete control over exactly what information gets printed, making it a flexible tool for creating hard-copy reports from your automated tasks.