News, Product Information, and Tips
Check out our free utlities in the downloads section

Set default printer from a script

by Steve Wiseman on August 9, 2007 · 9 comments

in Windows

I helped do some IT work at an Insurance company over the weekend. A friend of mine is an IT manager there. I will take any chance I can to poke around a real life corporate network (Instead of playing around with a farm of Virtual Machines)…so it was worth the extra work.

One of the problems they had was a dead printer. We needed to move a group of users to an alternate one and needed an easy way to set the default printer without knowing all the passwords to everyone’s account.

After a quick search I came up with this VB Script:

Set WSHNetwork = CreateObject(“WScript.Network”)
WSHNetwork.SetDefaultPrinter “[PRINTER_NAME]“

We updated the script so [PRINTER_NAME] was the name of the printer we wanted as our default. To find the correct name, simply open the Printers and Faxes icon in the control panel.

Printers and Faxes

Use the exact words you see under the printer. So for example, if I wanted to set the Laserjet 4500 as my default, the script would look like this:

Set WSHNetwork = CreateObject(“WScript.Network”)
WSHNetwork.SetDefaultPrinter “HP Color LaserJet 4500″

Then I saved the script to a network shared and named it update.vbs (Share name was \\serverdc\printer)

In the users logon script I added this line:

cscript.exe \\serverdc\printer\update.vbs

Now when the users login they will have the new printer as their default.

Like this article? Then sign up for my newsletter to get free tips and software sent right to your inbox once a week. Like you, I hate spam – I will never spam, or sell your email address.

Related Articles:

{ 9 comments… read them below or add one }

1 cypherx January 22, 2010 at 9:11 am

Help!

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

c:\defprinter.vbs(1, 31) Microsoft VBScript compilation error: Invalid character

Windows 7 Professional x64.

Set WSHNetwork = CreateObject(“WScript.Network”)
WSHNetwork.SetDefaultPrinter “IT-PRINTER”

2 Steve Wiseman January 27, 2010 at 3:56 pm

This is from the blogging software we use here. You will need to erase the quotes and re-write them yourself…the quotes in the blog are a special kind that are not recognized by the script engine.

3 Adam February 16, 2010 at 10:58 am

Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

c:\dp2.vbs(2, 1) Microsoft VBScript runtime error: Object required: ‘WSHNetork’

Help!

Thank You

4 Steve Wiseman February 16, 2010 at 11:04 am

You have a typo WSHNetork should be WSHNetwork

5 Steve K. June 22, 2010 at 12:53 pm

There’s some other items to consider for that network printer. This should be the full blown script if you were to have users in a multi-server environment. I don’t see success in using that script for a multi-server environment.

Batch Script
~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~
@ECHO OFF
::Replace CompanyName With your company’s real name
TITLE COMPANYNAME LOGON SCRIPT
:: echo off will hide the actions of the logon scripts
:: and allow you to leave a message of your own.
ECHO. ADDING PRINTER
Cscript \\serverdc\sysvol\domain.com\scripts\AddPrinter.vbs
ECHO. PERFORMING OTHER ACTIONS
::Add additional commands here.
~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~
You can remove all of the lines that use ::, they are just notes for anyone using the script.

AddPrinter.vbs
~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~
Set WSHNetwork = CreateObject(“WScript.Network”)
WSHNetwork.AddWindowsPrinterConnection “\\serverdc\HP Color LaserJet 4500″
WSHNetwork.SetDefaultPrinter “\\serverdc\HP Color LaserJet 4500″
~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~

Store them in this location:
~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~
\\ServerDC\netlogon
~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~
The local drive location on the domain controller is C:\Windows \ Sysvol \ Sysvol \ Domain.com \ Scripts

Group Policy Deployment
~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~
User Configuration \ Windows Settings \ Scripts (Logon/Logoff) \ Logon
~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~
Open Logon properties, add \\serverdc\netlogon

Obviously replace serverdc with your own real Domain Controller in your business throughout all of the steps here.

6 Steve K. June 22, 2010 at 12:55 pm

I’m sorry, objNetwork should be WSHNetwork, I don’t normally use WSH network but I was trying to make it as close to your script as possible.

7 Steve Wiseman June 22, 2010 at 1:36 pm

Thanks for the post. Really appreciated. I have updated your code so it has WSHNetwork for the objects.

8 Trev August 11, 2010 at 8:53 am

Can you advise on the syntax for Windows 7 due to the UAC restrictions my Pre-Win 7 script doesnt work. any thought on getting this to run as admin with out hard coding the admin user s password??

9 Trev August 12, 2010 at 4:03 am

reference my previous comments..
After some additional research I have noted that you can not disable the UAC on the fly
I will have to look at getting the script run as ADMIN some how.
if anyone has any additional thoughts… please post.
PS: my script takes a 3 digit entry and uses that to create the 2 and 3 octect of an IP to generate the ip addy of the printer.
The driver make and model is pre defined in the script so it looks in the printer repository fromt eh printer driver. if it cant find it it downloads the driver from the F&P server and begins the install… names the printer and logs the event inc. time dates and signed on user.

Leave a Comment

t