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 – Set Default Printer Script

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 https://computerperformance.co.uk/
‘ Version 1.4 – April 24th 2010
‘ —————————————————-‘
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 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 –  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
‘ Script to set default printer .
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.1 – April 24th 2010
‘ —————————————————-‘
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.

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

I like thePermissions 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

Employ PowerShell for Printer Logon Scripts

 You may have noticed that VBScript is being superseded by PowerShell.  While PowerShell is used mainly for configuring the operating system, it’s possible to employ its cmdlets to add network printers. The technique is to create a ComObject, which can act as a wrapper for familiar VBScript commands. Here is example of PowerShell’s New-Object cmdlet setting the default printer:

# PowerShell Example to Set Default Printer
Clear-Host
$PSPrint = $(New-Object -ComObject WScript.Network)
$PSPrint.objNetwork.SetDefaultPrinter("Epson")

You could save these instructions in a .ps1 file.  However, the hard part is executing  this .ps1 file as a logon script.  See more about PowerShell logon scripts.

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