Logon Scripts

VBS Logon Script - AddWindowsPrinterConnection

Introduction to Logon Printer Script

Users love to print.  One of the classic jobs of a Windows logon scripts is to provide applications such as Word with a suitable network printer.  I will show you how to write a logon script in VBS using the AddWindowsPrinterConnection method.  The best client to test the script is XP, however the VBScript code also works with Windows 2003 and even NT.4. However, Windows 9x are more difficult because they need a of copy the driver on the client.

Topics for Printer Scripts

 ♦

Our MissionExample  Printer VBScript with AddWindowsPrinterConnection

Our mission is to create a script, which provides the user with a shared printer.  As so often, the script mimics your manual actions, in this instance adding a network printer in the Printers (and Faxes) folder. The concept is similar to mapping a network drive, but creating a printer rather than a home directory. Instead of a drive letter in explorer, the users get an icon in the Printers folder.

Map 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.  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 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.

Pre-requisites to Map 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 or get a script editor such as OnScript (free download).
  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.  For instance, this basic script needs no commas.  automatically.

Note 2: The AddWindowsPrinterConnection only requires one 'argument' the UNC path of the printer.

Note 3: 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 4: I deliberately made Example 1 as short as possible so that it would highlight the essential commands.  Normally, as you will see in example 2, I include a header section and declare variables.

Guy Recommends SolarWinds' Free Network MonitorSolarwinds Network Device Monitor

Thus utility makes it easy to check the health of a router or firewall.  Check the real-time performance, and availability statistics, for any device on your network.  Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl - it's free. Download your free Network Device Monitor

If you need more comprehensive network analysis software:
Download a free trial of NPM (Network Performance Monitor)

Example 2 - Printer Script with AddWindowsPrinterConnection

This script has the same core 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 - Connect a network printer with AddWindowsPrinterConnection
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.2 - April 24th 2010
' -----------------------------------------------------------------'
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 printer script.

VBS 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.

Note 3: To help with the line numbers, and to color code your commands get OnScript (free download).

Guy Recommends: Permissions Analyzer - Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like the Permissions Monitor because it enables me to see quickly WHO has permissions to do WHAT.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource.  Give this permissions monitor a try - it's free!

Download Permissions Analyser - Free Active Directory Tool

Example 3 - Windows 9x with AddWindowsPrinterConnection

AddWindowsPrinterConnection vbs logon ScriptsPre-requisites for AddWindowsPrinterConnection

  1. 1. This is the same basic script as example 1 and 2.  The difference in this example, is that Windows 9x adds an extra argument, which specifies the printer driver. Incidentally, I found the name of the printer driver buy looking on the advanced tab, Drivers. (See diagram)
  2. You must have already installed the driver on the client.  I know this is a terrible limitation, but then, I don't recommend Windows 9x in the first place.

Instructions for a Windows 9x Printer Logon Script

  1. Copy and paste the script below into notepad.
  2. Change the server name from "\\LittleServer to the name of your print server.
  3. Save the file with .vbs extension e.g. PrintersWin98.vbs.
  4. Double click and check in your Printers folder.

' PrintersWin98.vbs - Windows Logon Script.
' VBScript - to map a network printer
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.4 - April 24th 2010
' ----------------------------------------------------'
Option Explicit
Dim objNetwork, strUNCPrinter, strPrnDrv
strUNCPrinter = "\\LittleServer\win2pdf"
strPrnDrv = "HP LaserJet Stylus 800"

Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter, strPrnDrv
WScript.Echo "Check the Printers folder for : " & strUNCPrinter

WScript.Quit

' End of Guy's AddWindowsPrinterConnection logon script.

Learning Points

Note 1: Only use Example 3 if you have Windows 9x clients.

Note 2: Unlike the previous examples, you need a second argument for the driver, and a comma.

Note 3: There is also an option parameter to specify the LPT port, but the default LPT1 suits most installations so remember it is only an option. For example instead of strUNCPrinter, strPrnDrv, you need three parts  strUNCPrinter, strPrnDrv, strPort.

Summary- Printer AddWindowsPrinterConnection

Mapping Printers is a classic job for a Windows logon script.  All you need to test this script is a shared network printer, the key method is AddWindowsPrinterConnection.  The script works on XP, Windows 2003 and NT 4.0 clients.  However, if you have Windows 9x machines, they as usual they need extra arguments.

This page was designed to get you started mapping network printers.  For your next step, see below, or in the menus to left how you can set the default printer and perform other printer scripting tasks.

 

Download my Logon Script eBook for only $6.25

Logon ScriptThe extra features you get in your eBook include, more pages full of detailed examples.  Also, ten 'how to...' sections, with screen shots showing which menus to use.  Go for Guy's eBook - and get a printable version with copy enabled and no expiry date.

  Jumbo Script 7 Package

See more printer logon scripts examples

Logon Script Home   • Printer computer   • Remove printer logon script

AddWindowsPrinterConnection script   • Multiple printers    • Assign Logon Script AD

• EnumPrinterConnections   • SetDefaultPrinter script   • Free Import CSV Tool

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: