Login to participate
Endpoint Management & Virtualization BlogsRSS

Set Default Printer

trb48's picture

I needed to find a way to set the default printer via a Deployment Console job. I have found a few different ways of doing it.

The first is a vbs script:

Set WSHNetwork = CreateObject("WScript.Network")
WSHNetwork.SetDefaultPrinter "[PRINTER_NAME]"

For more info check out this web site: http://www.intelliadmin.com/blog/2007/08/set-defau...

The second can be used in the command prompt:

start /W RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "[Printer_Name]"

For more information check out this web site: http://www.robvanderwoude.com/2kprintcontrol.php

You can even enter in the server location for a printer, it needs to be in this format: \\Path\Printer_Name

This script works well and most importantly it does the job.

lah's picture

Set Default Printer

I have done something similar. I use the script below in GPO.
GPO -> User Configuration -> Windows Settings -> Scripts (Logon/Logoff) -> Logon.
In the "Add a Script" dialog, type DefaulPrinter.vbs in "Script Name" and type \\server\printer in "Script Parameters".

' DefaultPrinter.vbs 
' --- Start Script --- 
On Error Resume Next
strComputer = "."
strDefaultPrinter = Wscript.Arguments.Item(0)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Local = TRUE")
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strDefaultPrinter
objNetwork.SetDefaultPrinter strDefaultPrinter
' --- End Script ---
adamli9's picture

Multiple users

I believe the default printer setting is user-specific, so keep that in mind if multiple users use the machine.

trb48's picture

I have my default printer

I have my default printer name stored in a registry key.  Is there a way to create a VBS script that will query the key, set the keys value as a variable, and stick that variable into the script above?

I have done it with a BAT file:

:: delims is a TAB followed by a space
FOR /F "tokens=3 delims= " %%A IN ('REG QUERY "HKLM\Software\Path\To\Key" /v DefaultPrinterName') DO SET DefaultPrinterName=%%A
ECHO Name=%DefaultPrinterName%

ECHO Installing the Printer (%DefaultPrinterName%)...

start /W RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%DefaultPrinterName%"

Any ideas on how to do it with a VBS script?

lah's picture

Query Registry using vbs

 

This vbs does the same thing as your bat. You need to make som changes in the script if the registry key is not located in HKEY_LOCAL_MACHINE and the value is not a string value.

The Hey, Scripting Guy! Archive have been very useful for me. It makes learning vbs so much easier.

HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Test"
ValueName = "Printer"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strDefaultPrinter
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Local = TRUE")
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strDefaultPrinter
objNetwork.SetDefaultPrinter strDefaultPrinter