• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2493
  • Last Modified:

Adding TCP/IP printers with batch

Win2000 and XP workstations need new printers installed

Everyones printer and faxes window is cluttered with old unused printers that need to be removed.
Original configuration was a TCP/IP port directly (to the printer with static IP address) from each workstation.
Then I was told to go to Print Shares on the server. Which I did.
This current configuration the queue's are always getting jammed and I spend a lot of my day unjamming them.
They would now like me to add a new TCP/IP port by (host name of the printer) because the printers no longer have static IP address and are now configured by DHCP.

What I do now is:
Add printer
New printer
Create a new port
Standard TCP/IP port
printer name or tcp/ip address (I enter the host name here)

How can I automate this? I've got over 200 computers to do.

First I'd like to remove ALL current printers on machine weather they be attached to the server or ported directly to the printer and then have a batch or some other script to automate the above printer install.

1 Solution
It appears like all this information is stored in the registry. I looked for one of my printers and found it under:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print

You could create a printer and then export the registry entry to a file. You could maybe create all the printers you want to install and export to a file at the Print level.

Then you could use "reg delete" to delete all printers and then "reg import" to save the new entries.

Good Luck,
Lee W, MVPTechnology and Business Process AdvisorCommented:
I think you have other problems if your printers are getting jamed.  I had a network of 600+ Windows PCs and one print server and things rarely jammed.  Of course, it does depend on your environment, but these things are supposed to que up so that they all print out over time.  In addition, you could have setup Printer Pooling if you have identical printers.  Then whichever printer was free would spit out the document(s).

In my opinion, putting TCP/IP printers on workstations directly - especially when there's more than a few workstations, is a tremendous waste of time and effort.

That aside, the Windows Management Instrumentation (WMI) should be able to do what you are asking.  If you wanted to script it, I'd suggest studying up on that (unfortunately, I never used it myself, but one of my former coworkers did write a script using WMI that changed print servers and removed old dead printers.
herfe's a vbscript that will add a printer.
there is some commented out lines (witn the preceding '  )
if you want to hard code it, just add more printerpath lines followed by the AddWindowsPrinterConnection calls

dim WshShell, WshNetwork, PrinterName, default

'printerName = InputBox("Which printer do you want to install :adcptXX","Which printer?","adcpt60")

'default = MsgBox("do you want this as your default printer?", vbYesNo ,"Default Printer?")
Set WshNetwork = WScript.CreateObject("WScript.network")

'to hard code put printername inside the quotes and remove the ampersand and 'PrinterName'
PrinterPath = "\\printserver\" & PrinterName  
WshNetwork.AddWindowsPrinterConnection PrinterPath

'if default = y then
'WshNetwork.SetDefaultPrinter PrinterPath

'End If

and here is a script that will remove all printers that are mapped..  if you need it...

(plagerized from http://www.dbforums.com/archive/index.php/t-774023.html  )


set wshNetwork = CreateObject ("WScript.Network")
'deletes all network printers
Set clPrinters = WshNetwork.EnumPrinterConnections
On Error Resume Next
For i = 0 to clPrinters.Count - 1 Step 2
wshNetwork.RemovePrinterConnection clPrinters.Item(i+1), true

you could add this as part of the login script   (put the remover first...)
and configure the addprinter script with the printers that you want the users to have..

let me know if this helps.  

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

* You can check these scripts if they are useful for you :


 * Install a printer using rundll32.exe :
You can use psexec to execute this command with admin rights :
Pstools Suite found at http://www.sysinternals.com

 * Psexec run like the RunAs command.
The user doubli-click a shortcut and psexec executes your Rundll command with the account you want.
You need to choose the Rundll32 you want to run.

 * A script to use with Rundll32 :
'Beginning of VbScript
'Save in a .vbs file
'Add the paths where needed:
Set WshShell = WScript.CreateObject("WScript.Shell")
Command = "psexec -u Domain\Admin -p password " & CHR(34) & "rundll32.exe" & CHR(34) & "syntax"
msgbox Command
WshShell.Run Command, 1, false
'End of VbScript

The VB Script can be encoded.
Or create a user for the occasion.

 * You can use Sysdiff to find the registry changes. It is in the Resource Kit.
Addendum for Sysdiff :
HOW TO: Programmatically Create a Printer and Printer Port by Using PrinterAdmin (Prnadmin.dll) with a Visual Basic Script in Microsoft Windows 2000 :

Original article :

Find normally Prnadmin.dll in the Windows 2000 Resource Kit.
You can add printers using batch. the easiest way is to rename your computer names so they include an id for the area which they  reside and then printer script assigns printers based on the area from the computername. when i get to work tomorrow will post the script i use that functions in this manner.  
YohahehoAuthor Commented:

Sorry this took so long. I just got it figured out. It’s a bit long but saves a lot of time in the end. I’ll just lay out snips of the 3 batch files I created. You could probably combine them into one if you wanted to but I left it in 3 so you could easily see the 3 different functions that would be performed. I pushed these batch files out with psexec and Altiris.

Thanks for the help

Batch file # 1 deleting all the old printers.
:: The "PrintUIEntry" is case sensative!!!
:: This does not remove the driver or the port just the printer from the printers window
:: At the begining of the cmd window it states that UNC paths are not supported but is
:: still runs for me :)

:: This is to remove a locally installed printer (TCP/IP or LTP)
:: The printer name at the end of each line is the name as it appears in the printers page

rundll32 printui.dll,PrintUIEntry /dl /q /n "acrobat distiller"
rundll32 printui.dll,PrintUIEntry /dl /q /n "hp4050 - rm142"
rundll32 printui.dll,PrintUIEntry /dl /q /n "hp4100 - media center"

:: This is to remove a network mapped printer from a server
:: (This removes printers from the local workstation not the server!)
:: The printer name at the end of each line is the path and name as it appears on the SERVER!!!

rundll32 printui.dll,PrintUIEntry /dn /q /n "\\myserver\hp4100-media center"
rundll32 printui.dll,PrintUIEntry /dn /q /n "\\myserver\hp4100-rm101-107"

Batch file # 2 Adding printer ports to a computer
:: This batch file installs a registry key to add TCP/IP and or host name ports to a computer
:: You can edit the reg key and add your own schools ports
:: This must run with Admin rights
:: At the begining of the cmd window it states that UNC paths are not supported but it
:: still runs for me :)

@echo off
regedit /s "\\myserver\Utilities\printer ports.reg"
net stop spooler
Ping -n 5>nul
net start spooler

Here is a snip from the reg file.
Windows Registry Editor Version 5.00
rem Read the remed out statements just below for information on how to add your own ports.

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports]

rem In each of the following keys there is only 2 places you need to make changes so this file will suit your
rem school. At the end of the first line change that to an IP_Address or host name of printer. Then on either
rem the 4th or 5th line you will have to enter the same information depending on whether you are adding a port
rem by TCP/IP or by printer host name.

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_10.247.110.13]
"SNMP Community"="public"
"SNMP Enabled"=dword:00000001
"SNMP Index"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\npi317a70]
"SNMP Community"="public"
"SNMP Enabled"=dword:00000001
"SNMP Index"=dword:00000001

Batch file # 3 Adding printers and drivers
:: The port must already be on the computer for this to add the printer!
:: At the begining of the cmd window it states that UNC paths are not supported but is
:: still runs for me :)

:: /b = "Name people will see in printers window"
:: /f = "path to printer driver .inf file"
:: /r = "host name of printer"
:: /m = "printer driver model name"

rundll32 printui.dll,PrintUIEntry /if /b "Media Center" /u /f "\\myserver\Utilities\Printer drivers\4200w2k&xp drivers\hp4200p6.inf" /r "npib36c08" /m "HP Laserjet 4200 PCL 6" /z

:: I put this in because if you keep clicking the batch file it will produce multiple copies of the printer.
:: This will make sure that doesn't happen. Make sure in quotes it has the same name as the /b switch uses
:: in the line above.

rundll32 printui.dll,PrintUIEntry /dl /q /n "Media Center (copy 2)"
rundll32 printui.dll,PrintUIEntry /dl /q /n "Media Center (copy 3)"

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now