Skip to main content

How to Get the Number of Pending Printer Jobs in Batch Script

In a high-intensity office or a warehouse running automated labelers, a "Stalled" print queue is a sign of trouble. If your system thinks it's printing but the physical paper isn't moving, the Job Count will begin to climb. By monitoring the number of pending jobs, you can build a Batch script that acts as an "Early Warning System", sending you an alert or restarting the print spooler automatically if more than five jobs are stuck in the queue. Rather than waiting for a frustrated user to call, your script can detect the bottleneck the moment it happens.

This guide will explain how to count pending print jobs using Batch.

Method 1: The Quick Statistics Check (PowerShell)

PowerShell provides reliable, structured access to print job counts for every printer on the system.

@echo off
setlocal

echo.

powershell -NoProfile -Command ^
"$printers = Get-Printer -ErrorAction SilentlyContinue;" ^
"if (-not $printers) { Write-Host '[INFO] No printers found on this system.'; exit };" ^
"$printers | ForEach-Object {" ^
" $jobs = Get-PrintJob -PrinterName $_.Name -ErrorAction SilentlyContinue;" ^
" $count = if ($jobs) { @($jobs).Count } else { 0 };" ^
" [PSCustomObject]@{ Printer = $_.Name; PendingJobs = $count }" ^
"} | Format-Table Printer, PendingJobs -AutoSize"

echo.
pause
endlocal

Method 2: Detecting a Printing "Stall"

This script checks a specific printer. If the job count is greater than zero, it flags the operator to verify the printer is functioning.

@echo off
setlocal

set "Printer=Warehouse_Zebra"

echo [STATUS] Checking job count for %Printer%...

:: Use PowerShell to get a reliable job count for a specific printer
set "Count=0"
for /f %%a in ('powershell -NoProfile -Command ^
"$jobs = Get-PrintJob -PrinterName '%Printer%' -ErrorAction SilentlyContinue;" ^
"if ($jobs) { @($jobs).Count } else { 0 }"') do (
set "Count=%%a"
)

echo Current jobs for %Printer%: %Count%

if %Count% gtr 0 (
echo [ALERT] There are %Count% job(s^) waiting. Ensure the printer has paper and is online.
) else (
echo [OK] Queue is empty.
)

pause
endlocal

Method 3: The "Auto-Recovery" Guard

If any printer's job count stays above a certain threshold, this script restarts the spooler to attempt automatic recovery.

@echo off
setlocal enabledelayedexpansion

set "MaxJobs=5"

echo [GUARD] Checking for stalled print queues (threshold: %MaxJobs% jobs^)...

set "AlertTriggered=0"

for /f "tokens=1,2 delims=|" %%a in ('powershell -NoProfile -Command ^
"Get-Printer | ForEach-Object {" ^
" $jobs = Get-PrintJob -PrinterName $_.Name -ErrorAction SilentlyContinue;" ^
" $count = if ($jobs) { @($jobs).Count } else { 0 };" ^
" if ($count -gt 0) { $($_.Name)|$count }" ^
"}"') do (
if %%b gtr %MaxJobs% (
echo [CRITICAL] %%a has %%b pending jobs (exceeds threshold of %MaxJobs%^)
set "AlertTriggered=1"
)
)

if "!AlertTriggered!"=="1" (
echo.
echo [RECOVERY] Restarting Print Spooler...
net stop spooler >nul 2>&1
timeout /t 3 /nobreak >nul
net start spooler >nul 2>&1
if !errorlevel! equ 0 (
echo [SUCCESS] Spooler restarted. Monitor the queue for improvement.
) else (
echo [ERROR] Spooler restart failed. Manual intervention required.
)
echo %date% %time% - Spooler restarted due to queue exceeding %MaxJobs% jobs >> print_recovery.log
) else (
echo [OK] All print queues are within normal limits.
)

endlocal

How to Avoid Common Errors

Wrong Way: Thinking "JobCount" means the printer is out of paper

A high job count could mean many things: the printer is out of ink, there is a paper jam, it's disconnected from the network, or the user sent a corrupted file that the computer is struggling to "Translate" for the printer.

Correct Way: Use Method 1 to see the global state. If all printers have pending jobs, the problem is likely your Local Spooler service. If only one printer is backed up, the problem is that specific hardware.

Problem: WMIC Output Formatting

WMIC output often contains hidden carriage return characters that can break a Batch if comparison, producing errors like 0 was unexpected at this time.

Solution: Use PowerShell (Methods 1-3) to retrieve clean, structured data. PowerShell returns properly formatted values without the hidden characters that plague WMIC-to-batch parsing.

Best Practices and Rules

1. Identify "System" Jobs

Some PDF printers or "Microsoft Print to PDF" instances might hold onto a job until you choose a filename. These will show up in your count.

2. Time-Based Auditing

Run a script every 10 minutes. If the job count doesn't move (it stays at 3 for three consecutive checks), you have a "Dead" queue that needs a restart.

3. Log the Bottlenecks

Log the peak job count for each day. This helps you identify which office printers are being "Overworked" and might need to be upgraded to a higher-capacity model.

Conclusions

Monitoring the number of pending print jobs via Batch script is a professional way to maintain high productivity in any Windows environment. By moving from reactive "Break-Fix" management to proactive status monitoring, you ensure that your document flow remains smooth and error-free. This automated oversight is essential for maintenance on specialized hardware and preventing small jams from turning into department-wide printing outages.