Many methods of programmatically working with printers exist and I’ll be going over the main ones we use on the Windows platform support teams in this blog. These can be real lifesavers for print server administrators if changes need to be made across many queues.
As a quick caveat, please keep in mind that we use many of these scripts and utilities for troubleshooting during the course of support incidents but not all of them are supported. I’ll explicitly call out which tools are eligible for Microsoft support services and which ones are not while we walk through them.
In-Box Print Admin Scripts
The print management visual basic scripts provided with the OS are fully supported.
ü These scripts are WMI-based and thus are only for use against a stand-alone print server.
ü The first versions of these scripts were included with the Windows Server 2003 Resource Kit.
ü The Resource Kit versions of these scripts are NOT supported but work against a cluster.
· Windows XP / Server 2003 – %windir%\system32
· Windows Vista / 7 / 2008 / 2008R2 – %windir%\system32\printing_admin_scripts
Configures or displays configuration information about a printer.
Adds, deletes, and lists printer drivers.
Pauses, resumes, cancels and lists print jobs.
Adds, deletes, and lists printers or printer connections, in addition to setting and displaying the default printer.
Creates, deletes, and lists standard TCP/IP printer ports, in addition to displaying and changing port configuration.
Prints a test page, pauses or resumes a printer, and clears a printer queue.
Publishes a print queue to active directory.
Built-in Self Help
All of the in-box scripts display syntax information and examples if you run them without parameters.
Commonly used commands:
Add HP Universal Printer Driver (UPD) 5.3 PCL 6 driver
cscript prndrvr.vbs -a -m "HP Universal Printing PCL 6 (v5.3)" -v 3 -i "C:\TEMP\hpcu115u.inf" -h "C:\TEMP"
Add HP UPD 5.3 Postscript (PS) driver
cscript prndrvr.vbs -a -m "HP Universal Printing PS (v5.3)" -v 3 -i "C:\TEMP\hpcu115v.inf" -h "C:\TEMP"
Add Xerox GPD 2.1 PS driver
cscript prndrvr.vbs -a -m "Xerox GPD PS V2.1" -v 3 -i "C:\TEMP\x2UNIVP.inf" -h "C:\TEMP"
Delete all printer drivers that are not in use
cscript prndrvr.vbs -x
Set a print queue to RAW only (disable Advanced Printing Features)
cscript prncnfg.vbs -t -p printer +rawonly
Set a print queue to print directly to the printer
cscript prncnfg.vbs -t -p printer +direct
Create a Standard TCP/IP port with SNMP status disabled
cscript prnport.vbs -a -md -r IP_127.0.0.1 -h 127.0.0.1 -o raw -n 9100
Print a test page to a queue
cscript prnqctl.vbs -p "HP Color LaserJet" -e
Cancel all print jobs in a queue
cscript prnqctl.vbs -p "HP Color LaserJet" -x
One-line PrintUI.dll commands are fully supported by Microsoft support services, but the in-box print admin scripts are always the preferred method of getting the same tasks done.
PrintUI.dll contains a wealth of information and examples if you run the following command.
RUNDLL32 PRINTUI.DLL,PrintUIEntry /?
ü Use the /u switch when connecting to queues or drivers may be downloaded repeatedly.
ü Adding a local port (standard tcp/ip or otherwise) is NOT supported by this tool.
ü An undocumented /x switch is required to map an IPP printer. (Content ID 314486)
ü PrintUI.dll is updated somewhat frequently, so make sure you’re using the latest version.
Commonly used commands:
Connect to a print queue:
rundll32 printui.dll,PrintUIEntry /u /in /n \\machine\printer
Add OEM printer driver using inf:
rundll32 printui.dll,PrintUIEntry /ia /m "Xerox WorkCentre 7775 PS" /h "x64" /v 3 /f C:\Xerox\x2DLEXP.inf
Add in-box printer driver:
rundll32 printui.dll,PrintUIEntry /ia /m "Brother DCP-116C" /h "x64" /v 3
The vast majority of PrintUI.dll problems are related to incorrect or incomplete syntax.
ü Use example syntax as a template.
ü PrintUI.dll switches are case sensitive.
ü Driver names must be exactly as you see them in the GUI. (when installed manually)
ü Drivers require specification of the correct processor architecture using the /h switch. Use a simple “x86” or “x64” parameter to indicate your preference.
ü Drivers also require a version to be specified with the /v switch. You will always use 3 as the parameter for this switch. (There are no supported v2 kernel mode drivers today.)
SetPrinter (SetPrinter API wrapper)
SetPrinter Support Guidelines
SetPrinter.exe is distributed with the Windows Server 2003 Resource Kit and is subject to the Resource Kit Support Policy.
Resource Kit Support Policy
The SOFTWARE supplied in the Windows Resource Kit Tools is not supported under any Microsoft standard support program or service. Customers can, however, report issues and bugs by sending e-mail to firstname.lastname@example.org. Microsoft will, at its sole discretion, address issues and bugs reported in this manner, and responses are not guaranteed. This e-mail address is only for issues related to the Windows Resource Kit Tools and the Windows Deployment and Resource Kits.
The SOFTWARE (including instructions for its use and all printed and online documentation) is provided "AS IS" without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the SOFTWARE and documentation remains with you.
In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the SOFTWARE be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the SOFTWARE or documentation, even if Microsoft has been advised of the possibility of such damages.
Although no support services are available for this tool, it generally works with all OS’s, both clustered and stand-alone, from Windows XP to Windows Server 2008 R2.
A few sample commands are provided later in this document, but consumers of this tool will generally need to use the embedded help to customize the command line for a desired result.
ü A custom script (e.g. VBScript, PowerShell, etc.) is a good alternative if SetPrinter will not accomplish a specific goal or functions in an unexpected manner.
Running SetPrinter.exe without any arguments will display the embedded help.
Syntax: SetPrinter [-<mode>] <\\server|printer> <level> [<data> …] [<cmd>]
or SetPrinter -help <level>
or SetPrinter -examples <level>
or SetPrinter -show [-<mode>] <\\server|printer> <level>
\\server : (or \\server\*) change all local printers on this server
(use "" or "*" for all printers on local machine)
\\server\\: change server (not printer) settings
printer : change this printer (printer or \\server\printer)
level : PRINTER_INFO level (0 – 9).
data : (optional) Level specific data in ‘keyword=value keyword=value’
format. Data is unchanged for keywords not specified.
cmd : (optional, but must be last if present) one of:
"pause", "resume", "purge", "setstatus"
-help : show format of data for <level>
-examples : show usage examples for <level>
-show : show current settings (no changes applied – all <data> ignored)
-<mode> : (optional) Only valid on Windows Vista and later.
Possible values: -cached, or -notcached
(See documentation for OpenPrinter2 API, pOptions parameter)
The <level> determines which members of the printer structure are used.
The most common <level> values are:
Help for each level is available by running SetPrinter –help <level>
If there is a specific setting you are interested in, you may configure a print queue as desired in the GUI and then use the SetPrinter –show option to find the value.
These scenarios are intended to “teach you to fish”.
I need to know what setting controls landscape vs. portrait in the Printing Defaults, and then set that to landscape for all print queues.
1) This is a global setting that I want all clients to get when the print queue is connected to for the first time, so 8 is the appropriate level.
2) Run SetPrinter –help 8 to see the level-specific help.
3) Run SetPrinter –examples 8 to see some examples.
4) Create a printer called HP and configure it to print in portrait mode.
5) Run setprinter -show "HP" 8.
6) Use the GUI to reconfigure the HP printer to print in landscape mode.
7) Run setprinter -show "HP" 8.
8) Compare the SetPrinter –show results to discover dmOrientation has changed from 1 to 2.
9) Roll out the change to all print queues with SetPrinter "” 8 pDevMode=dmOrientation=1.
I need to know what setting controls a print processor, and then switch all queues to WinPrint.
1) This is a general setting for the print queue, so 2 is the appropriate level.
2) Run SetPrinter –help 2 to see the level-specific help.
3) Run SetPrinter –examples 2 to see some examples.
4) Create a printer called Xerox and set it to the WinPrint print processor.
5) Run setprinter –show “Xerox” 2.
6) Use the GUI to reconfigure the Xerox printer to use a 3rd party print processor.
7) Run setprinter –show “Xerox” 2.
8) Compare the SetPrinter –show results to discover pPrintProcessor has changed.
9) Roll out WinPrint to all print queues with SetPrinter "" 2 pPrintProcessor=WinPrint.
Note: Most 3rd party drivers work fine with WinPrint. When in doubt, ask your OEM.
I need to mirror the security settings on one print queue to all other queues.
1) This is a security setting for the print queue, so 3 is the appropriate level.
2) Run SetPrinter –help 3 to see the level-specific help.
3) Run SetPrinter –examples 3 to see some examples.
4) Create a printer called Lexmark and set security as desired.
5) Run setprinter –show “Lexmark” 3 and copy the security descriptor.
6) Roll out the security descriptor to all print queues with SetPrinter "" 3 <Security Descriptor>.
Note: You may need to run this as the local system account. (psexec –s –i cmd.exe)
TechNet Script Repository
The TechNet Script Repository provides printer management scripts with support provided through the forums. Whether you’re a developer or a “script kiddie”, this is the place to go for all of your custom printer management scripting needs. Maybe you can even contribute something to the community? J
TechNet Script Center Repository
Disclaimer: The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
I hope you’ve enjoyed reading this information as much as I have putting it together for you, and I sincerely hope one or more of these options can save you some time in the near future.