Map a Network Printer and also set the Default Printer
Introduction to
AddWindowsPrinterConnection Logon Script
One of the jobs of a Windows logon script is to provide applications such as Word or Excel with a suitable network printer. I will show you how to build a VBScript that uses the AddWindowsPrinterConnection method to create a printer. As a bonus, I have a second script that adds code to set the default printer. You probably cannot help spotting similarities between these scripts and mapping a network drive in section 5. For example, CreateObject("WScript.Network")
Topics for AddWindowsPrinterConnection Scripts
Our MissionExample 1 Simple Printer Script with AddWindowsPrinterConnection Example 2 Full Printer Script with AddWindowsPrinterConnection Example 3 - SetDefaultPrinter
Printer Logon Script Summary
Our Mission
Our mission is to create a script, which provides the user with a connection to a shared printer. As so often happens, the script mimics your manual actions, in this instance adding a network printer in the Printers (and Faxes) folder.
Printer Methods
Printer VBScripts follow the classic format of object, method, and value. We create the network object called objNetwork. Next, we employ the
AddWindowsPrinterConnection method to create the network printer. Finally, we get a new printer icon in the Printers and Faxes folder, which matches the value set in strUNCPrinter. What gives any scripting language its power is the methods or verbs, in this case AddWindowsPrinterConnection, in other scripts
SetDefaultPrinter or even RemovePrinterConnection.
Example 1
-Simple Printer Script with
AddWindowsPrinterConnection
Our objective is to create a printer icon, which connects to the network printer called \\ zara\ HP LaserJet. Printer scripts employ a variety of different methods; in this instance, we concentrate on AddWindowsPrinterConnection. If you have
Windows 2003 or XP clients, the procedure is straightforward; this method only needs one parameter or argument, namely the UNC path to the shared printer. For example:
AddWindowsPrinterConnection strPrinterUNCPath
Note that in the case of Windows 2003 and XP clients, there is no argument to specify the driver; the client will automatically download the correct driver from the print server.
Introduction to VBScript.doc By Guy Thomas
Page 30 of 59 Pre-requisites for Mapping Printers
You need a machine (server) with a shared printer.
Instructions for a simple Printer Logon Script
1. Copy and paste the script below into notepad.
2. Change the server name from "\\ zara to the name of your print server. 3. Create a shared printer and use that name in place of \ HP LaserJet in my
script.
4. Save the file with .vbs extension e.g. Printers.vbs. 5. Double click and then launch your Printers folder.
'
' Printers.vbs - Windows Logon Script.
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection " \\ zara\ HP LaserJet"
Learning Points
Note 1: I do realize that scripts go wrong. However, with printer scripts, it s often the result of 'over-think', so begin with simple plan and pay attention to the syntax. In this instance, there is one method AddWindowsPrinterConnection and one parameter or argument, the UNC path to the network printer "\\ zara\ HP LaserJet".
Note 2: This basic script needs no commas. I mention this as another example of keeping it simple, don't go into 'over-think' and add unnecessary commands.
In addition, there is no need for an argument to set the printer driver or port number, the server will take care of that business automatically.
Note 2: This script conforms to the classic VBScript structure, object, method, and value. We create a network object, objNetwork, apply the
AddWindowsPrinterConnection method and then assign the value of your shared network printer.
Note 3: I deliberately made Example 1 as short as possible so that it would highlight the essential commands. Normally, I include a header section and declare variables, as you will see in Example 2.
Introduction to VBScript.doc By Guy Thomas
Page 31 of 59
Example 2
-Printer Script with AddWindowsPrinterConnection
This script has the same core commands as the first example but with a few extra coding niceties. As you get to know my VBScript style, so you will see my
familiar heading section, liberal use of variables and a WScript.Echo message box to confirm what has happened.
' PrintersLong.vbs - Windows Logon Script. ' VBScript - to map a network printer
' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.3 - September 2005
' ---' Option Explicit
Dim objNetwork, strUNCPrinter strUNCPrinter = "\\ zara\ HP LaserJet."
Set objNetwork = CreateObject("WScript.Network") objNetwork.AddWindowsPrinterConnection strUNCPrinter WScript.Echo "Check the Printers folder for : " & strUNCPrinter WScript.Quit
' End of Guy's Windows logon script.
Learning Points
Note 1: Option Explicit forces us to declare variables before we use them in the VBScript. The idea is to reduce spelling mistakes.
Note 2: One reason that I like to employ variables is to make it easier to troubleshoot in general and display messages with WScript.Echo in particular.
Example 3
-SetDefaultPrinter
As you may know, I believe in building up scripts in stages. However, once you have created two or three printers you probably want to control which printer is the default. Thus, setting a default printer is a natural progression, and any production script would have both methods, AddWindowsPrinterConnection and SetDefaultPrinter.
Scripting printers is easy because there are fewer parts to each command. In the case of SetDefaultPrinter, we need only the name of the printer - that is all. Here is the SetDefaultPrinter method and its one argument:
Introduction to VBScript.doc By Guy Thomas
Page 32 of 59 Instructions to SetDefaultPrinter
1. Copy and paste the script below into notepad.
2. Save the file with .vbs extension e.g. SetDefaultPrinter.vbs.
3. Double click the script and check your Printers folder. Which is printer is now the default.
'
' SetDefaultPrinter.vbs - Windows logon script example ' PrintersDefault.vbs - Set the default printer
' VBScript - to map a network printer
' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.4 - April 24th 2005
' ---' Option Explicit
Dim objNetwork, strUNCPrinter
strUNCPrinter = "\\ zara\ HP LaserJet 2420"
Set objNetwork = CreateObject("WScript.Network") objNetwork.AddWindowsPrinterConnection strUNCPrinter ' Here is where we set the default printer to strUNCPrinter objNetwork.SetDefaultPrinter strUNCPrinter
WScript.Echo "Check the Printers folder for : " & strUNCPrinter WScript.Quit
' End of Guy's Windows logon example VBScript.
Learning Points
Note 1: The basic SetDefaultPrinter is a short command with no commas and only one argument - the printer share name.
Note 2: Surprisingly, you do not need to know the whereabouts of the print server.
Note 3: To see this script to best effect you need a third printer, which is initially set to the default printer. The good news is that you can play 'fantasy printers'. Just call for the printer wizard and pretend that you have an Epson xyz or a Lexmark 123ABC, just to practice your scripting.
Summary of AddWindowsPrinterConnection
Mapping Printers is an important application of VBScript. All you need to experiment with this script is a shared network printer. Incidentally, in a
production script it costs little to add an extra line of code, which explicitly sets the default printer.
From a pure scripting point of view, this script teaches the VBScript rationale of creating an object and then using methods to perform useful tasks such as
connecting to a network printer and then setting the default printer.
This page covered the following VBScript methods, commands and syntax:
Key VBScript terms: AddWindowsPrinterConnection, SetDefaultPrinter, Key method:
Introduction to VBScript.doc By Guy Thomas
Page 33 of 59