1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Setting Default Printer

Discussion in 'Scripting & Programming' started by Nelix, Sep 13, 2008.

  1. Nelix
    Honorary Member

    Nelix Gigabyte Poster

    Hi All

    Where I work the printers are pushed out to the machines via a script that maps the printer in the room that thay are currently in, not the best method I know. My problem is that the script does not set the default printer.

    I know that it is possible to do this when installing a printer on a per user basis but cant seem to find a way of doing it on a machine basis so that whoever logs on to the machine gets the default set for them, does anyone know how this can be done?


    Certifications: A+, 70-210, 70-290, 70-291
    WIP: 70-294
  2. wagnerk
    Highly Decorated Member Award

    wagnerk aka kitkatninja Moderator

    Here's the script that I use, I would say that it's mine, but it's a script that I've modified from a few sources, lol.

    We set printers based on the OU that the PC is in:

    on error resume next 
    Dim objPrinter13
    Set objPrinter13 = CreateObject("WScript.Network") 
    'Define printers to install 
    'V IMPORTANT. DO NOT user Share names to install printers. Use the full printer name as it appears on the servers printer menu. 
    'Enter "none" into printers array to install no printers. 
    'Do not leave array empty 
    'network paths should use the "\" character not the "/" character 
    'Warning if the printer path is invalid then an error message will be displayed 
    'Items in the array MUST be enclosed by speach marks and be seperated by commas. 
    ' check for computer membership of ou 
    ' leave off any DC settings as these are not needed 
    If isMemberOu("ou=ouGroup") Then 
       printers = Array("[URL="file://\\server_name\printer1","\\server_name\Printer2"]\\server_name\printer1","\\server_name\Printer2[/URL]") ' define the printers you want to install 
       defaultPrinter = "[URL="file://\\server_name\printer1"]\\server_name\printer1[/URL]" ' Define the default printer 
       installPrinters = True ' Allows the install routines to run Set to false to disabled printer instalation 
       objPrinter.SetDefaultPrinter "[URL="file://\\server_name\printer1"]\\server_name\printer1[/URL]" 'this sets the default printer on the PC
    'Define another ou 
    elseIf isMemberOu("ou=anotherOuGroup") Then 
       printers = Array("[URL="file://\\server_name\printer3"]\\server_name\printer3[/URL]") ' define the printers to install 
       defaultPrinter = "[URL="file://\\server_name\printer3"]\\server_name\printer3[/URL]" ' Define the default printer 
       installPrinters = True ' Allows the install routines to run Set to false to disabled printer instalation 
       objPrinter.SetDefaultPrinter "[URL="file://\\server_name\printer3"]\\server_name\printer3[/URL]"
       'Continue with the elseif statements if you have more OU's
    END IF 
    On Error Resume Next    
    IF installPrinters Then 
       'install printers 
       Set objWSHNetwork = CreateObject("WScript.Network") 
       Set WS_PRINTERS = objWSHNetwork.EnumPrinterConnections 
       Set WshShell = WScript.CreateObject("WScript.Shell") 
       FOR EACH printer in printers 
          if LCase(printer) <> "none" Then 
          END IF 
       'set Default Printer 
       if LCase(defaultPrinter) <> "none" Then 
          objWSHNetwork.SetDefaultPrinter defaultPrinter 
       END IF 
       Set WS_PRINTERS = Nothing 
       Set WS_NET = Nothing 
       Set WshShell = Nothing 
    END IF 
    ' Determine OU Membership of computer 
    Private Function isMemberOu(checkOu) 
       Set objSysInfo = CreateObject("ADSystemInfo") 
       strComputerDN = objSysInfo.ComputerName 
       intIndexStart = InStr(LCase(strComputerDN), "ou=") 
       intIndexLength = InStr(LCase(strComputerDN), ",dc=") - intIndexStart 
       isMemberOu = false 
       If intIndexStart > 0 Then 
          compOu = LCase(Mid(strComputerDN, intIndexStart, intIndexLength)) 
       End If 
            If InStr(1, compOu, checkOu, 1) Then 
          isMemberOu = true 
       END IF 
       Set objSysInfo = nothing 
    End Function 
    The two lines, I believe, that you're missing are these two:

    1. defaultPrinter = "\\server_name\printer1"
    2. objPrinter.SetDefaultPrinter "\\server_name\printer1"

    Of course, I assuming... as I haven't seen your printer script.

    Certifications: CITP, PGCert, BSc, HNC, LCGI, PTLLS, MCT, MCITP, MCTS, MCSE, MCSA:M, MCSA, MCDST, MCP, MTA, MCAS, MOS (Master), A+, N+, S+, ACA, VCA, etc... & 2nd Degree Black Belt
    WIP: PGDip
  3. Nelix
    Honorary Member

    Nelix Gigabyte Poster

    Cheers Ken

    Not sure if this would work.

    We install printers depending on location in AD, the script installs the printers to the machine and not just to the currently logged on user, heres a snipit from the script

    On Error Resume Next
    ' Bits for setting up log file.
    strDirectory = "C:\Windows\Log"
    strFile = "\Build.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Const ForAppending = 8
    Set objTextFile = objFSO.OpenTextFile _
    (strDirectory & strFile, ForAppending, True)
    strdate = Date()
    strtime = Time()
    ' for log off
    Set ws = WScript.CreateObject("WScript.Shell")
    ' Section to get PC name
    Dim oNet, pcname
    Set oNet = CreateObject("WScript.Network")
    pcname = UCase(oNet.ComputerName)
    'Wscript.Echo "This PC is called: " & pcname
    ' Section to query AD for location
    Set objComputer = GetObject _
    objProperty = objComputer.Get("Location")
    If objProperty = "" Then
    '    Wscript.Echo "The location has not been set."
    ' Section to ask if you want to change location
    title = "Computer Location"
    retval = msgbox("The computer location has not been set!" & vbNewLine _
     & vbNewLine _
     & "Do you want to change the computer location now?",vbYesNo)
    if retval = vbNo then
    ws.Run("C:\Windows\System32\shutdown /l"),0,True 
    ' Section to change location
    title2 = "Enter Room Number"
    message2 = "Enter the room number this computer will be in." & vbNewLine _
     & vbNewLine _
     & "This should not be prefixed with Dept code." & vbNewLine _
     & vbNewLine _
     & "For example: IT1"
    strLocation = UCase(InputBox(message2, title2))
    objComputer.Put "Location" , strLocation
    objTextFile.WriteLine("This computer's location was update in AD to "  & strLocation & " on the " & strdate & " at " & strtime)
    Wscript.Echo "Location changed. You will now be logged off. Please then log back on to install printers!"
    ws.Run("C:\Windows\System32\shutdown /l"),0,True
    End If
    '    Wscript.Echo "This computers location is: " & objProperty
    End If
    ' Section to install printers based on location
    If objProperty = "TR1" Then
    sCmd = "rundll32.exe printui.dll,PrintUIEntry /ga /n [URL="file://\\PrintServer\Printername"]\\PrintServer\Printername[/URL]"
    'WScript.Echo sCmd
    Set oShell = CreateObject("WScript.Shell")
    oShell.Run sCmd, 1, True
    End If
    This script runs whenever a specific user logs on, it does not run for everyone. I know this is a strange way of doing it but I didn't have any say in the matter during implementation.

    This script also checks for the PC location and if not set it will ask you to set it before installing Printers.

    The bit that is missing is the bit to set the printer as default for everyuser that logs onto the machine, I have been told that this is not possible because the printer is being installed to the machine and not the a user, can't believe that that is true. :biggrin

    Hope someone can help

    Certifications: A+, 70-210, 70-290, 70-291
    WIP: 70-294
  4. ThomasMc

    ThomasMc Gigabyte Poster

    what about this

    '  Example VBScript to set the local Default Printer
    '  Guy Thomas February 2004.
    '  ******************************
    Option Explicit
    Dim objPrinter
    Set objPrinter = CreateObject("WScript.Network")
    objPrinter.SetDefaultPrinter "\\ServerName\PrinterName"
    ' End of example VBScript 
    Just read the full post :oops:, hopefully Fergals about as am sure this is right up his street.
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)

Share This Page