Logon Scripts

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator


VBS Logon Scripts - SetDefaultPrinter

Introduction to SetDefaultPrinter

Whether you have recently added a printer, or just removed one, I expect that you want to set the default printer.  Naturally, if there is only one printer, then Windows XP will automatically make that one the default and there will be less need for a SetDefaultPrinter section in your Windows logon script.

Topics for SetDefaultPrinter

SetDefaultPrinter Arguments

Compared with scripting Active Directory, 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:

objNetwork.SetDefaultPrinter "HP LaserJet 2420"
or
objNetwork.SetDefaultPrinter strUNCPrinter

You will see in my logon script examples that SetDefaultPrinter is a straightforward VBScript method that can be used on all clients from XP and Windows 2003 to NT 4.0 and Windows 9x.

Example 1 - SetDefaultPrinter

With other examples, I have provided additional steps to first create a printer, then manipulate it.  Since setting the default printer is so easy, and follows on so logically, here is one complete script, which creates a printer and then sets that printer as the default.

Instructions to SetDefaultPrinter

  1. This script works on XP, Windows Server 2003, and Windows 2000.
  2. Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
  3. Save the file with .vbs extension e.g. SetDefaultPrinter.vbs.
  4. Double click and launch your Printers folder.  Check which is printer is 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 = "\\LittleServer\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 appreciate this SetDefaultPrinter script, I suggest that you create another printer and manually set that as the default.  Now run this script again and see what happens.

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  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

Example 2 -  SetDefaultPrinter to a different printer

Scenarios:
Suppose that your XP computer has 4 printers. What if you you just delete the default printer from the default Printers and faxes folder?  Now there are 3 printers remaining, which should become the new default?  Do you leave it to chance, or do you script it?  If it were me I would want the control, therefore, I would run up a SetDefaultPrinter VBScript.

Take another scenario, suppose that you are adding a printer and you don't want it to be the default, you want to enforce one of the others as the default.  Then again, the answer is to script it.

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 and check in your Printers folder.  Which printer is 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 2.1 - April 24th 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork, strUNCPrinter, strLocal
strUNCPrinter = "\\LittleServer\HP LaserJet 2420"
strLocal = "IBM"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter

' Here is where we set the default printer to strUNCPrinter
objNetwork.SetDefaultPrinter strLocal
WScript.Echo "Check the Printers folder for : " & strUNCPrinter

WScript.Quit

' End of Guy's SetDefaultPrinter logon script.

Learning Points

Note 1:  This time strLocal points to a different printer from strUNCPrinter.

Note 2:  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 pretend that you have an Epson xyz or a Lexmark 123ABC, just to practice your scripting.

Summary - SetDefaultPrinter

It costs very little in terms of scripting time or space, to add an extra line of code to explicitly set the default printer.  SetDefaultPrinter only needs one argument, the name of the UNC path to the print server.  This is one of the easiest Windows VBScripts that you will encounter.  For a more demanding challenge try EnumPrinterConnections.  (See below for links).


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 Also

Logon Script Home  ● RemovePrinterConnection   EnumPrinterConnections

 *


Google

Web  This website

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved.

Please report a broken link, or an error.