Script to automate adding and removal of printers (and set additional preferences)

Posted on 2008-10-28
Last Modified: 2013-11-10
Hi, I'm looking for a script (batch / wsh / ?) that will automate the adding and removal of network printers. In addition, it would be great to have the script set particular printing preferences, such as to set the Job Type to Hold and to set the Hold Print Details User ID for the user logged in.

Your help will be appreciated!
Question by:cyrax
  • 2

Accepted Solution

Angelized earned 250 total points
ID: 22840606
Hello, I have something for you that can be a start of an autamtic installation of printer based on AD Computer object.
What you need to do is to set your print server name : strPrintServer = "."  
The script will list all printer on the print server and compare the computer description in ad.  If the computer description contains the name of the printer it will be installed on the computer.
This really a 1st raw, lemme know if you need more informartion on this.

strPrintServer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & PrintServer & "\root\cimv2")

Set objComputer = CreateObject("Shell.LocalMachine")

sComputername = objComputer.MachineName

Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer")

For Each objPrinter in colInstalledPrinters

    If instr(ReturnComputerDescription(sComputername),objPrinter)>0 Then


    End If       


Function InstallPrinter (sPrintServer,sPrinter,Default)

   Set WshNetwork = CreateObject("WScript.Network")

   Dim FullPrinterString

   FullPrinterString = "\\" & sPrintServer & "\" &  sPrinter

   WshNetwork.AddWindowsPrinterConnection FullPrinterString

   If default=True Then WshNetwork.SetDefaultPrinter FullPrinterString

End Function

Public Function ReturnComputerDescription (sCpuname)

	On Error Resume Next

	Set objComputer = GetObject(QueryActiveDirectory("sCpuname"))

	objProperty = objComputer.Get("Description")

	If IsNull(objProperty) Then

	    ReturnComputerDescription = "The description has not been set."


	    WReturnComputerDescription = objProperty

	    objProperty = Null

	End If

End Function	

Public Function QueryActiveDirectory(sCpuname)

'Function:      QueryActiveDirectory

'Purpose:       Search the Active Directory's Global Catalog for users

'Parameters:    UserName - user to search for

'Return:        The user's distinguished name


    Dim oAD 'As IADs

    Dim oGlobalCatalog 'As IADs

    Dim oRecordSet 'As Recordset

    Dim oConnection 'As New Connection

    Dim strADsPath 'As String

    Dim strQuery 'As String

    Dim strUPN 'As String

    Set oRecordSet = CreateObject("ADODB.Recordset")

    Set oConnection = CreateObject("ADODB.Connection")

    'Determine the global catalog path

    Set oAD = GetObject("GC:")

    For Each oGlobalCatalog In oAD

        strADsPath = oGlobalCatalog.ADsPath


    'Initialize the ADO object

    oConnection.Provider = "ADsDSOObject"

    'The ADSI OLE-DB provider

    oConnection.connectiontimeout = 15

    oConnection.Open "ADs Provider"

    'Create the search string

    'MsgBox strADsPath

    strQuery = "<" & strADsPath & _

      ">;(&(objectClass=computer)(Name=" & _

      sCpuname & "));location,cn,distinguishedName;subtree"

        'Execute the query

    'MsgBox strQuery

    Set oRecordSet = oConnection.Execute(strQuery)

    If oRecordSet.EOF And oRecordSet.BOF Then

       'An empty recordset was returned

        QueryActiveDirectory = "Not Found"

    Else    'Records were found; loop through them

        While Not oRecordSet.EOF

            QueryActiveDirectory = oRecordSet.Fields("distinguishedName")



    End If


    Set oConnection = Nothing

    Set oRecordSet = Nothing

End Function

Open in new window


Expert Comment

ID: 22841415
Btw : how to delete printer
Function RemovePrinter (sPrintServer,sPrinter)

   Set WshNetwork = CreateObject("WScript.Network")

   Dim FullPrinterString

   FullPrinterString = "\\" & sPrintServer & "\" &  sPrinter

   WshNetwork.RemovePrinterConnection FullPrinterString   

End Function

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now