cmd add printer conditionals

Posted on 2011-10-17
Last Modified: 2012-05-12
I've successfully gotten my script file to add network printers to each PC when a user logs on, however I want to clarify a few things that I'm struggling with.
I need a way to essentially check to see if those printers are already installed first, and if they are, end the script, if not, then delete the old network printers from the old server, and install these new ones. Whats happening now is every time the user logs on, whether the printers are installed or not, they reinstall anyway. I know I'm probably better in vbs, but this is all I have to work with right now.

Here is my current script for the printers, excluding my mapped drive cmd lines..

REM Add printer
 RunDll32.EXE printui.dll,PrintUIEntry /in /n "\\servername\TOSHIBA e-STUDIO7030cPRO PCL 6"
REM Set Default Printer
 RunDll32.EXE printui.dll,PrintUIEntry /y /n "\\servername\TOSHIBA e-STUDIO7030cPRO PCL 6"

REM Add printer
 RunDll32.EXE printui.dll,PrintUIEntry /in /n "\\servername\TOSHIBA e-STUDIO5530cPRO RPCS"

REM Add printer
 RunDll32.EXE printui.dll,PrintUIEntry /in /n "\\servername\TOSHIBA e-STUDIO5530cPRO PS"

I'm not super talented at scripts and I've tried to read the long lists of cmds that MS puts out or general users, but its all starting to run together and I just need someone to at least help me with the general structure =\.

Thanks for any help you can provide!

Question by:deanavey
    LVL 13

    Accepted Solution

    Just change the from_sv and to_sv to reflect your new and old servers
    It will also set the default printer to whatever the user had set
    Option Explicit
    Dim from_sv, to_sv, PrinterPath, PrinterName, DefaultPrinterName, DefaultPrinter
    Dim DefaultPrinterServer, SetDefault, key
    Dim spoint, Loop_Counter
    Dim WshNet, WshShell
    Dim WS_Printers
    DefaultPrinterName = ""
    spoint = 0
    SetDefault = 0
    set WshShell = CreateObject("")
    from_sv = "\\sms-bus" 'This should be the name of the old server.
    to_sv = "\\sms-fp" 'This should be the name of your new server.
    'Just incase their are no printers and therefor no defauld printer set
    ' this will prevent the script form erroring out.
    On Error Resume Next
    key = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
    DefaultPrinter = LCase(WshShell.RegRead (key))
    If Err.Number <> 0 Then
    DefaultPrinterName = ""
    'If the registry read was successful then parse out the printer name so we can
    ' compare it with each printer later and reset the correct default printer
    ' if one of them matches this one read from the registry.
    spoint = instr(3,DefaultPrinter,"\")+1
    DefaultPrinterServer = left(DefaultPrinter,spoint-2)
    if DefaultPrinterServer = from_sv then
    DefaultPrinterName = mid(DefaultPrinter,spoint,len(DefaultPrinter)-spoint+1)
    end if
    end if
    Set WshNet = CreateObject("WScript.Network")
    Set WS_Printers = WshNet.EnumPrinterConnections
    'You have to step by 2 because only the even numbers will be the print queue's
    ' server and share name. The odd numbers are the printer names.
    For Loop_Counter = 0 To WS_Printers.Count - 1 Step 2
    'Remember the + 1 is to get the full path ie.. \\your_server\your_printer.
    PrinterPath = lcase(WS_Printers(Loop_Counter + 1))
    'We only want to work with the network printers that are mapped to the original
    ' server, so we check for "\\Your_server".
    if LEFT(PrinterPath,len(from_sv)) = from_sv then
    'Now we need to parse the PrinterPath to get rhe Printer Name.
    spoint = instr(3,PrinterPath,"\")+1
    PrinterName = mid(PrinterPath,spoint,len(PrinterPath)-spoint+1)
    'Now remove the old printer connection.
    WshNet.RemovePrinterConnection from_sv+"\"+PrinterName
    'and then create the new connection.
    WshNet.AddWindowsPrinterConnection to_sv+"\"+PrinterName
    'If this printer matches the default printer that we got from the registry then
    ' set it to be the default printer.
    if DefaultPrinterName = PrinterName then
    WshNet.SetDefaultPrinter to_sv+"\"+PrinterName
    end if
    end if
    Set WS_Printers = Nothing
    Set WshNet = Nothing
    Set WshShell = Nothing

    Open in new window

    LVL 43

    Assisted Solution

    by:Steve Knight
    OK couple of things.  If you do want to use batch still and this method you could add a flag to say it has been setup, also you could amend a little to make it easier on the eye with some variables, and add it to a subroutine to call for each one to add:


    @echo off
    set printutil=RunDll32.EXE printui.dll,PrintUIEntry
    set srv=\\servername

    REM Keep a log file in the printers directory of each user profile to say if that printer is installed
    md "%userprofile%\printers" 2>NUL

    REM call :addprinter for each printer name, add default after it to make it the default
    call :addprinter "TOSHIBA e-STUDIO7030cPRO PCL 6" default
    call :addprinter "TOSHIBA e-STUDIO5530cPRO RPCS"
    call :addprinter "TOSHIBA e-STUDIO5530cPRO PS"

    echo Other things to run here after printers installed

    exit /b

      if not exist "%userprofile\printers\%~1.txt" (
        %printutil%  /in /n "%srv%\%~1"
        echo Installed %date %time% >
        if "%~2"=="default" %printutil% /y /n "\\%srv%\%~1"
    exit /b
    LVL 43

    Expert Comment

    by:Steve Knight
    Sorry missed the bit about deleting the old printers... v.messy in command, better in VBS like has been said :-)

    Author Closing Comment

    Thanks for the help guys!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    #Citrix #Citrix Netscaler #HTTP Compression #Load Balance
    If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
    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 …
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now