How to Print a Test Page in Batch Script
When setting up a new office printer, the "Last Mile" of verification is the Test Page. It proves that the driver is correct, the connection is stable, and the ink/toner is actually reaching the paper. Rather than walking through the Windows GUI and clicking "Print Test Page" for every workstation you set up, a Batch script can trigger this command programmatically. This is an essential step for automated deployment scripts, as after your script adds the printer, it prints a test page automatically, giving you physical "Receipt" of a successful installation.
This guide will explain how to trigger a printer test page using the command line.
Method: The "PrintUI" Test Page Call
The printui.dll utility is the only native way to trigger the official Windows Test Page (the one with the colorful Windows logo and diagnostic text).
@echo off
set "PrinterName=Office_LaserJet_P1102"
echo [ACTION] Sending Test Page to: %PrinterName%...
:: Verify the printer exists before sending a test page
wmic printer where "Name='%PrinterName%'" get Name >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Printer not found: %PrinterName%
echo Use "wmic printer get Name" to list available printers.
pause
exit /b 1
)
:: /k = Print a test page
:: /n = Specify the printer name
rundll32 printui.dll,PrintUIEntry /k /n "%PrinterName%"
echo [INFO] Test page sent to queue. Check the physical printer for output.
pause
Method 2: Printing a Custom Text File
Sometimes you don't want the "Official" test page; you just want to print a simple text file to verify basic character alignment.
@echo off
set "TargetPrinter=\\Server\ColorHP"
set "TestFile=%TEMP%\test_print.txt"
:: Generate a simple test document
echo ============================================ > "%TestFile%"
echo Test Print Successful >> "%TestFile%"
echo Date: %date% >> "%TestFile%"
echo Time: %time% >> "%TestFile%"
echo Printer: %TargetPrinter% >> "%TestFile%"
echo ============================================ >> "%TestFile%"
echo [ACTION] Sending text file to %TargetPrinter%...
:: Verify the printer exists
wmic printer where "Name='%TargetPrinter%'" get Name >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Printer not found: %TargetPrinter%
del "%TestFile%" >nul 2>&1
pause
exit /b 1
)
:: Use the print command to send the text file
print /d:"%TargetPrinter%" "%TestFile%"
if %errorlevel% equ 0 (
echo [INFO] Text file sent to printer queue.
) else (
echo [ERROR] Print command failed. Check the printer connection.
)
:: Clean up the temporary file
del "%TestFile%" >nul 2>&1
pause
Method 3: The "Installation Gate" Loop
Use this in a script for field technicians. The script adds the printer and then waits for the technician to confirm the test page actually came out.
@echo off
set "PrinterPath=\\PrintServer\New_MFP"
echo [SETUP] Adding printer: %PrinterPath%...
:: Add the network printer
rundll32 printui.dll,PrintUIEntry /in /n "%PrinterPath%"
:: Verify the printer was added
timeout /t 3 >nul
wmic printer where "Name='%PrinterPath%'" get Name >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Printer could not be added. Check the path and server.
pause
exit /b 1
)
echo [INFO] Printer added. Sending test page...
:: Send the test page
rundll32 printui.dll,PrintUIEntry /k /n "%PrinterPath%"
echo.
echo [VERIFICATION] A test page should be printing on %PrinterPath%.
set /p "success=Did it print correctly? (Y/N): "
if /i "%success%"=="Y" (
echo [SUCCESS] Printer installation verified.
) else (
echo [ALERT] Troubleshooting required. Rolling back installation...
rundll32 printui.dll,PrintUIEntry /dn /n "%PrinterPath%"
:: Verify the rollback
timeout /t 2 >nul
wmic printer where "Name='%PrinterPath%'" get Name >nul 2>&1
if %errorlevel% neq 0 (
echo [INFO] Printer has been removed.
) else (
echo [WARNING] Printer may still be listed. Manual removal may be needed.
)
)
pause
How to Avoid Common Errors
Wrong Way: Using the "Type" command with a network path
Commands like type MyFile.txt > \\Server\Printer only work for very old "Local" printers connected via parallel ports or if you have manually mapped the LPT port. It does not work for modern Wi-Fi or USB printers.
Correct Way: Use printui.dll (Method 1). It uses the full Windows Print Spooler and Driver stack, ensuring that the printer receives data in the format it expects.
Problem: Printer Name with Spaces
If your printer is named HP LaserJet 400 Color, the command will fail unless the name is in quotes.
Solution: Always use double quotes around your printer name: "%PrinterName%".
Best Practices and Rules
1. Identify "Default" Printer
If you don't provide a /n name to the printui command, it will usually fail. Unlike other commands, printui requires an explicit target for the test page.
2. Administrator Privileges
While users can usually print to their own printers, running diagnostic rundll32 calls sometimes requires Administrator elevation depending on your PC's security policy.
3. Clear the Queue First
If you've just fixed a paper jam, run your "Clear Print Queue" script before running the test page script. This ensures the test page doesn't get stuck behind 50 other broken documents.
Conclusions
Printing a test page via Batch script is the definitive way to "Close the Loop" on printer deployments. By moving from manual verification to automated diagnostic prints, you gain the ability to troubleshoot hardware issues instantly across your entire network. This professional level of verification ensures that your infrastructure is truly ready for business, providing the physical proof needed for any successful IT rollout.