Showing posts with label Adding Printers with VB script. Show all posts
Showing posts with label Adding Printers with VB script. Show all posts

Thursday 27 September 2012

Adding Printers with a Visual Basic Script

Adding Printers with a Visual Basic Script If you are a Network Administrator or work in a IT Role and it is your responsibility to install printers and maintain the network then this script will save you lots of time, if you take care of more than 10 computers you will know that having to install the same thing on each one can be pretty laborious, if you are running a Windows Server environment with a domain, you can use group policies to deploy your printers, Windows Server 2008 has a Deployed Printers option built in but can be sometimes unreliable, if you find that this is the case then you can use my script to deploy your printers.
We start out by opening notepad or any other txt file editor, on the first line we want to put in the following without the brackets,
(on error resume next) 
this tells the program when you encounter an error just continue at the next line. The next line we use the
Set objNetwork = CreateObject("WScript.Network") 

WScript.Network is a scripting object that provides methods and properties that allow for manipulation of network objects.
The next line you add in the printer
objNetwork.AddWindowsPrinterConnection "\\Server\Printer Shared Name"
after it has added the printer you will need to set it as the Default printer if you have more than one printer installed
objNetwork.SetDefaultPrinter "\\Server\Printer Shared Name"

So your finished script will look like this.
on error resume next
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\Server\Printer Shared Name"
objNetwork.SetDefaultPrinter "\\Server\Printer Shared Name"
Save this with the file extension of *.vbs for example addprinter.vbs and then you can use it as a script in the logon process with a group policy, or if you simply double click on the file it will install the printer for you, if you are deploying this with group policy you will want to remove it after successful installation as it will not work the second time around, unless you change some of the parameters, as it will already be installed it will give you an error, the change to the script that is required is below
'objNetwork.RemovePrinterConnection "\\Server\Printer Shared Name"
This goes above the objNetwork.AddWindowsPrinterConnection "\\Server\Printer Shared Name" line in the script so your script will now look like this below.

on error resume next
Set objNetwork = CreateObject("WScript.Network")
'objNetwork.RemovePrinterConnection "\\Server\Printer Shared Name"
objNetwork.AddWindowsPrinterConnection "\\Server\Printer Shared Name"
objNetwork.SetDefaultPrinter "\\Server\Printer Shared Name"