Link to home
Start Free TrialLog in
Avatar of NetAdmin2436
NetAdmin2436Flag for United States of America

asked on

Set default printer on this vbs script

Hey guys,
I'm not good at scripting. I've read that EnumPrinterConnections is a difficult technique.  But i found this script that will go through and delete all network printers and add network printers. Makes it nice if a printer get's changed. I added a call to my login script to start my PrinterScript.vbs when a user logs in. It works awesome...one problem though. The default printer. What do i add (and where) in script 1 to make the default printer the HPColorLaserJet4600PCL5c ?

script 1
********************************************************************************************
Set WshNetwork = CreateObject("WScript.Network")
Set wshNet = CreateObject("WScript.Network")
Set wshPrn = wshNet.EnumPrinterConnections
For x = 0 To wshPrn.Count - 1 Step 2
    If Left(wshPrn.Item(x+1),2) = "\\" Then wshNet.RemovePrinterConnection wshPrn.Item(x+1),True,True
Next
On Error Resume Next
Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\HPLaserJet5000PS"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server00\HPColorLaserJet4600PCL5c"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\Canon iR1600-2000 (FAX)"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\Canon iR1600-2000 PCL5e"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\HP DesignJet 800 24 by HP"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\hpLaserJet1300_PCL5e"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\HP LaserJet 6L"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)
 
**************************************************************************

I know i can make script 2 with the following to specify a default printer, just thought it would be easier and cleaner if it was just one script:

script 2
******************************************************************************
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'ScriptedPrinter'")

For Each objPrinter in colInstalledPrinters
    objPrinter.SetDefaultPrinter()
Next
******************************************************************************

What do i add (and where) in script 1 to make the default printer the HPColorLaserJet4600PCL5c using the EnumPrinterConnections technique ?
ASKER CERTIFIED SOLUTION
Avatar of sr75
sr75
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way, I typically use this script to remove network printers:

<script>
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")

objNet =  CreateObject("Wscript.Network")

strHost = "\\"                            

For Each objPrinter in colInstalledPrinters
     strPrinter = UCase(objPrinter.name)
     If l(strPrinter,2) = strHost then
               objNet.RemovePrinterConnection strPrinter
     end if
next

<end script>
Avatar of NetAdmin2436

ASKER

There was an extra " in the 7th line, but i deleted it and ran the script again.

Rock on! Worked like a charm.....I hope i never have to manually add another printer to a workstation ever again when a printer gets moved.

Thanks Very Much!!!
oops, yeah there was.  Glad it worked for you.