Hack 51 Automate Printer Management

figs/moderate.gif figs/hack51.gif

Here are a couple nifty ways to manage printers from the command line instead of via the GUI.

Managing printer mappings tends to be complicated task, especially in larger environments. Increased level of difficulty results from the fact that, in such situations, printers are shared (rather than used by individual users). Shared printer devices are typically network-attached (i.e., they either have internal network cards or are connected to external hardware-based print servers). This differs from a home/small office setup, where printing devices connect to individual workstations via parallel, USB, or infrared port.

The way printer software is installed also varies by connection. Local printers are either autodetected (in Windows 2000 and XP) or installed via the Add Printer wizard. In the case of network-attached devices, printers are first installed with the Add Printer wizard on a network server. Next, users connect to these printers (either by double-clicking on the printers' icons in My Network Places/Network Neighborhood or by running the Add Printer wizard), which triggers automatic download of printer drivers and their configuration on the local workstations. The printer mappings are stored as part of a user's profile.

Since the process of connecting to network printers is straightforward, you can leave this task to users. This is a viable solution, as long as printers are easy to find (e.g., by implementing a naming convention that clearly identifies the printer's location). This, however, is not always the case.


If you want to be able to manage printer mappings easily, you can use the CON2PRT command, which has been available since the release of the Zero Administration Kit for Windows NT 4.0. CON2PRT allows you to map network printers from the command line and is extremely easy to use. It works with Windows NT 4.0, 2000, and XP and can easily be included in a login script. Its only limitation is the fact that it cannot be used to force the installation of the locally attached printer, but this, fortunately, is rarely needed (since, with Windows 2000 and XP, local printers are usually autodetected).

The CON2PRT command offers three functions:


Deletes all existing printer mappings


Creates a new printer mapping


Creates a new printer mapping and sets it as the default

For example, to set a default printer to the printer LJ4000_PS_01 on the server SERVER01, you would type in the following:

CON2PRT /cd \\SERVER01\LJ4000_PS_01

You can find the complete syntax of CON2PRT by typing the standard /? switch at the command prompt, and download CON2PRT.EXE (along with the rest of the Zero Administration Kit for Windows) from http://www.microsoft.com/ntworkstation/downloads/Recommended/Featured/NTZAK.asp.


While CON2PRT is easy to use, its capabilities are limited to removing all printer mappings and creating new ones (including setting the default printer). Though it seems that this might be all you need when dealing with printers, Windows offers much wider range of functionality.

As you probably know, most of the features used by Windows in the traditional 32-bit Windows environment are implemented in the form of Dynamic Link Libraries (DLLs, files with the extension .dll). As the name indicates, DLLs are collections (libraries) of functions that can be used whenever they are needed (dynamically) by any process operating within Windows. Unfortunately, access to functions included in the DLL files, in general, is restricted primarily to programmers. However, there are exceptions to this rule. For example, you can take advantage of certain specifically designed DLLs by running the RUNDLL32 command that is included in every 32-bit version of Windows. Keep in mind, though, that the number of functions available with RUNDLL32 is fairly small (for example, it does not include any of the Win32 API calls exported from the system DLLs).

Printer-management functions used by RUNDLL32 are stored in the printui.dll file. To find out the collection of functions included in this file, you can run the following from the command prompt or StartRun box:

rundll32 printui.dll,PrintUIEntry /?

This will display a long list of options available to you. In general, you use the following syntax of commands (where options and commandfile parameters vary):

rundll32 printui.dll,PrintUIEntry options commandfile

Here are just a few of many possible uses of this command:

Delete a local printer (called HP LaserJet 5)

rundll32 printui.dll,PrintUIEntry /dl /n "HP LaserJet 5"

Delete the local printer on the remote computer (called RemotePC01)

rundll32 printui.dll,PrintUIEntry /dl /n "HP LaserJet 5" /c\\RemotePC1

Delete a network printer

rundll32 printui.dll,PrintUIEntry /dn /n "\\SERVERNAME\PRINTERNAME"

Add a network printer

rundll32 printui.dll,PrintUIEntry /in /n "\\SERVERNAME\PRINTERNAME"

Set a printer as the default

rundll32 printui.dll,PrintUIEntry /y /n "\\SERVERNAME\PRINTERNAME"

?Marcin Policht