Rob van der Woude's Scripting Pages

Command Line Printer Control

As of Windows 2000, many, but not all, printer settings can be configured from Windows's command line using PRINTUI.DLL and RUNDLL32.EXE.

The list of functions on this page was first generated using the following command in Windows 2000 (Professional), with later additions from Windows 7:

RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /?

  Notes:
Notes: (1) I certainly did not test each of these switches myself!
To be honest, I usually prefer to use the (VBScript) printer management scripts that come with Windows 2000 and later (look for %windir%\System32\*prn*.vbs).
  (2) In Windows 7, PRINTUI.EXE can be used as shorthand for RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry

More information is available at Microsoft's TechNet under Q189105.

Download Microsoft's PrintUI.DLL User's Guide and Reference for more detailed, task oriented explanations of command line options.

 

Usage:

RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry [ options ] [ @commandfile ]
/a[file] binary file name
/b[name] base printer name
/c[name] UNC machine name if the action is on a remote machine
/dl delete local printer
/dn delete network printer connection
/dd delete printer driver
/e display printing preferences
/f[file] either inf file or output file
/F[file] location of an INF file that the INF file specified with /f may depend on
/ga add per machine printer connections
/ge enum per machine printer connections
/gd delete per machine printer connections
/h[arch] driver architecture, one of the following:
  Windows 2000: Alpha | Intel | Mips | PowerPC
  Windows 7: x86 | x64 | Itanium
/ia install printer driver using inf file
/id install printer driver using add printer driver wizard
/if install printer using inf file
/ii install printer using add printer wizard with an inf file
/il install printer using add printer wizard
/in add network printer connection
/ip install printer using network printer installation wizard
/j[provider] print provider name
/k print test page to specified printer, cannot be combined with command when installing a printer
/l[path] printer driver source path
/m[model] printer driver model name
/n[name] printer name
/o display printer queue view
/p display printer properties
/q quiet mode, do not display error messages
/r[port] port name
/s display server properties
/Ss Store printer settings into a file
/Sr Restore printer settings from a file
  Store or restore printer settings option flags that must be placed at the end of command:
  2 PRINTER_INFO_2
  7 PRINTER_INFO_7
  c Color Profile
  d PrinterData
  s Security descriptor
  g Global DevMode
  m Minimal settings
  u User DevMode
  r Resolve name conflicts
  f Force name
  p Resolve port
  i Driver name conflict
/u use the existing printer driver if it's already installed
/t[#] zero based index page to start on
/v[version] driver version, one of the following:
Windows 95 or 98 | Windows NT 3.1 | Windows NT 3.5 or 3.51 | Windows NT 3.51 | Windows NT 4.0 | Windows NT 4.0 or 2000 | Windows 2000
/w prompt the user for a driver if specified driver is not found in the inf
/y set printer as the default
/Xg get printer settings
/Xs set printer settings
/z do not auto share this printer
/Y do not auto generate a printer name
/K changes the meaning of /h to accept 2,3,4 for x86 or x64 or Itanium and /v to accept 3 for "Type 3 - User Mode"
/Z share this printer, can only be used with the /if option
/? help this message
@file command line argument file
/Mw[message] show a warning message before committing the command
/Mq[message] show a confirmation message before committing the command
/W[flags] specifies flags and switches for the wizards (for APW & APDW)
  r make the wizards to be restart-able from the last page
/G[flags] specifies global flags and switches
  w suppress setup driver warnings UI (super quiet mode)

 

Examples:

Run server's printer ports properties:
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /s /t1 /n\\machine
Run server's printer drivers properties:
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /s /t2 /n\\machine
Run printer properties
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /p /n\\machine\printer
Run add printer wizard localy
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /il
Run add printer wizard on \\machine
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /il /c\\machine
Run queue view
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /o /n\\machine\printer
Run inf install
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /if /b "Test Printer" /f %windir%\inf\ntprint.inf /r "lpt1:" /m "AGFA-AccuSet v52.3"
Run add printer wizard using inf
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /ii /f %windir%\inf\ntprint.inf
Add per machine printer connection
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /ga /c\\machine /n\\machine\printer /j"LanMan Print Services"
Delete per machine printer connection
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /gd /c\\machine /n\\machine\printer
Enumerate per machine printer connections
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /ge /c\\machine
Add printer driver using inf (Windows 2000)
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /ia /c\\machine /m "AGFA-AccuSet v52.3" /h "Intel" /v "Windows 2000" /f %windir%\inf\ntprint.inf
Add printer driver using inf (Windows 7
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /ia /K /c\\machine /m "AGFA-AccuSet v52.3" /h "x86" /v 3 /f %windir%\inf\ntprint.inf
Remove printer driver (Windows 2000)
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /dd /c\\machine /m "AGFA-AccuSet v52.3" /h "Intel" /v "Windows 2000"
Remove printer driver (Windows 7)
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /dd /c\\machine /m "AGFA-AccuSet v52.3" /h "x86" /v "Type 3 - User Mode"
Set printer as default
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /y /n "printer"
Set printer comment
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xs /n "printer" comment "My Cool Printer"
Set printer port
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xs /n "printer" PortName "port:"
Get printer settings
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xg /n "printer"
Get printer settings saving results in a file
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /f "results.txt" /Xg /n "printer"
Set printer settings command usage
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xs /n "printer" ?
Store all printer settings into a file
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Ss /n "printer" /a "file.dat"
Restore all printer settings from a file
RUNDLL32.EXE PRINTUI.DLL,PrintUI /Sr /n "printer" /a "file.dat"
Store printer information on level 2 into a file
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Ss /n "printer" /a "file.dat" 2
Restore from a file printer security descriptor
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Sr /n "printer" /a "file.dat" s
Restore from a file printer global devmode and printer data
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Sr /n "printer" /a "file.dat" g d
Restore from a file minimum settings and resolve port name
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Sr /n "printer" /a "file.dat" m p
Enable Client Side Rendering for a printer (Windows 7)
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xs /n "printer" ClientSideRender enabled
Disable Client Side Rendering for a printer (Windows 7)
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xs /n "printer" ClientSideRender disabled

 

More to explore:

Microsoft Knowledge Base articles: How to add printers with no user interaction in Windows XP and How to add printers with no user interaction in Windows (2000/2003).

How to print files from the command line (or batch files).

 

A tip from Denis St-Pierre:

One of the gotchas was that RUNDLL32.exe and PRINTUI.exe always return Errorlevel=0 regardless of errors.
I needed a reliable means to test for the existence of a printer and this is the result:

REM NOTE: RUNDLL32.exe and PRINTUI.exe always return Errorlevel=0
REM The trick: Try to get the printer settings into a file
REM If No file is created = The Printer does not exist
SET PrinterName=FIT FP-32L Raster
SET TESTfile=%TEMP%\PrtExist.txt

REM Delete %TESTfile% to avoid false positives
DEL %TESTfile% /F /Q

REM Try to get the printer settings into a file
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xg /n "%PrinterName%" /f "%TESTfile%" /q

IF EXIST "%TESTfile%" (
	ECHO %PrinterName% printer exists
) ELSE (
	ECHO %PrinterName% printer does NOT exists
)

PAUSE

Thanks Denis

 

More RUNDLL and RUNDLL32 commands can be found on my RUNDLL page.

An overview of Registry Entries for Printing by Microsoft.

 

If RUNDLL32 just won't do the job:

Sometimes we need to do just alittle more than RUNDLL32 is capable of, like installing a TCP/IP printer port.
In that case, consider buying yourself a copy of the Windows 2000 Server/Windows Server 2003 Resource Kit and use PRNADMIN.DLL and the accompanying sample scripts written in VBScript.

My own AddIPPrn.bat uses these VBScripts to install a printer, its driver and a TCP/IP printer port on a remote computer.

 

If all you need is to backup/restore, clone or migrate existing printers:

Often the printer configurations of existing computers need to be cloned to other computers, or restored after reinstalling Windows.
In those cases, a backup/export and restore/import with Microsoft's PrintMig 3.1 may be the best (and easiest) solution.
Though earlier versions of PrintMig were GUI only, version 3.1 can also be run without user interaction from the command line or in a batch file:

Microsoft (R) Windows (TM) Printer Migrator 3.1
Copyright (C) 1997-2002 Microsoft Corp. All rights reserved

usage: PRINTMIG [options] [server]
       [-?] Display this message
       [-b] Backup - followed by CAB file name
       [-r] Restore - followed by CAB file name
       [-l] Attempt to convert LPR Ports to SPM
       [-i] Suppress warning popups.  Info still written to log file.

If server is not specified then the local machine is implied.

Example command line to restore an existing config to a server:
  printmig -r d:\print\ps1.cab \\prt-srvr1

Example command line to backup a server to the specified file:
  printmig -b "\\filesrv\store\print server 2.cab" \\prt-srvr2

PrintMig can be used for Windows (Server) 2000, XP and Server 2003.
For Windows 7 and Windows Server 2008, use the Printer Backup Recovery Migration tool (PRINTBRM) instead.
PRINTBRM is a great (native) command line tool to backup/restore and migrate printers.

Its command line couldn't be any simpler.
To backup:

%windir%\System32\Spool\Tools\PrintBRM.exe -s \\sourcecomputername -b -f filename.printerExport

And to restore:

%windir%\System32\Spool\Tools\PrintBRM.exe -s \\destinationcomputername -r -f filename.printerExport

For more help:

%windir%\System32\Spool\Tools\PrintBRM.exe /?

Export printer drivers:

With Microsoft's PrintMig tool you can create backups of your complete set of printers, ports, drivers, print processors and print monitors.
Unfortunately there is no way to selectively backup nor restore, so you may want to backup a clean test system with just the printers and components you need to migrate.

If you need to migrate a single printer, use my ExpPrnDr.bat (Export Printer Driver) instead.
It uses Microsoft's DEVCON.EXE to list all files belonging to a specific printer driver, and PRINTUI.DLL to install that printer driver on any computer.


page last modified: 2021-11-22; loaded in 0.0023 seconds