Skip to main content

How to Set the Default Printer in a Batch Script

In many office environments, especially with logon scripts or application deployment, you often need to programmatically set the default printer for a user. This ensures that print jobs from applications are automatically sent to the correct device without requiring manual configuration by the user. While there is no simple, single command for this, Windows provides a built-in utility that can be controlled from a batch script to manage printers.

This guide will teach you how to set the default printer using the standard RUNDLL32.EXE command with the PRINTUI.DLL. This is the classic and most direct method for this specific task. You will also learn how to first get a list of all available printers, which is a necessary prerequisite.

The Challenge: No Simple "Set-Default" Command

There is no single command like SETPRINTER /DEFAULT "My Printer". The default printer is a user-specific setting stored deep within the Windows profile and registry. To change it from the command line, we need to call a function inside one of the Windows printing system's own DLL files.

The Core Method: RUNDLL32.EXE PRINTUI.DLL

The RUNDLL32.EXE utility is a Windows program that can execute a function from within a DLL file. The PRINTUI.DLL is the library that contains all the functions for the Print User Interface. We can use this combination to programmatically manage printers.

Syntax: RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /y /n "Printer Name"

  • PRINTUI.DLL,PrintUIEntry: The specific DLL and the entry-point function we are calling.
  • /y: The switch to set the default printer.
  • /n "Printer Name": The name of the printer you want to set as the default. This name must be exact.

Step 1: Getting the Exact Printer Name

Before you can set a default printer, you must know its exact name as it appears in Windows. A typo or a slightly different name will cause the command to fail. The most reliable way to get this list in a script is with WMIC.

Command: WMIC PRINTER GET Name

This gives you a clean list of all installed printer names, as output.

Name
Microsoft Print to PDF
HP LaserJet Pro (Network)
Microsoft XPS Document Writer
Fax
note

You must use one of these exact strings (e.g., "HP LaserJet Pro (Network)") in your set-default command.

Step 2: The Command to Set the Default Printer

Once you have the exact name, you can execute the RUNDLL32.EXE command. This is a user-level setting and typically does not require administrator rights.

For example, this script sets the "HP LaserJet Pro (Network)" printer as the default for the current user.

@ECHO OFF
SET "TargetPrinter=HP LaserJet Pro (Network)"

ECHO Setting the default printer to: "%TargetPrinter%"

RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /y /n "%TargetPrinter%"

ECHO.
ECHO Operation complete.
ECHO To verify, run "WMIC PRINTER GET Name,Default" to see which printer is marked as TRUE.
warning

The command runs silently and, if successful, the change is immediate. There is no confirmation message.

Common Pitfalls and How to Solve Them

  • Incorrect Printer Name: This is the number one reason for failure. If the name you provide in the /n switch does not exactly match an installed printer, the command will fail silently.

    • Solution: Always verify the name first by running WMIC PRINTER GET Name. Copy and paste the name to avoid typos.
  • Printer is Not Installed: You cannot set a printer as the default if it is not installed for the current user. The command will fail silently.

    • Solution: Your script should first check if the printer exists before attempting to set it as the default. This is shown in the practical example below.
  • Handling Paths with Spaces (or Printer Names): The printer name itself can contain spaces.

    • Solution: Always enclose the printer name in double quotes in the /n switch: /n "%MyPrinter%".

Practical Example: A User Logon Script for Printers

This script is designed to run when a user logs in. It checks if a specific office printer is installed. If it is, the script sets it as the default printer. This ensures users in a specific office always have the correct default printer.

@ECHO OFF
SETLOCAL
SET "OfficePrinter=Main Office LaserJet"
SET "IsInstalled=0"

ECHO --- Default Printer Logon Script ---
ECHO.
ECHO Checking for the presence of the office printer...

REM --- Step 1: Check if the required printer is installed ---
FOR /F "tokens=*" %%P IN ('WMIC PRINTER GET Name') DO (
REM Clean the variable from WMIC output
SET "CurrentPrinter=%%P"
FOR /F "tokens=*" %%C IN ("!CurrentPrinter!") DO SET "CurrentPrinter=%%C"

IF /I "!CurrentPrinter!"=="%OfficePrinter%" (
SET "IsInstalled=1"
GOTO :Found
)
)

:Found
IF %IsInstalled% EQU 0 (
ECHO [INFO] The standard office printer is not installed. No action taken.
GOTO :End
)

ECHO Printer found. Setting it as the default...
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /y /n "%OfficePrinter%"

ECHO.
ECHO [SUCCESS] The default printer has been set to "%OfficePrinter%".

:End
ENDLOCAL
note

This script uses DelayedExpansion (!Var!) for the variable cleaning loop to work correctly.

Conclusion

The RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry command is the standard and most direct method for setting the default printer from a batch script.

For reliable printer management:

  1. First, get a list of exact printer names using WMIC PRINTER GET Name.
  2. Use the command RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /y /n "Printer Name" to set the default.
  3. For robust scripts, check if the printer is installed before you attempt to set it as the default.
  4. Always enclose the printer name in double quotes to handle spaces.

This technique is a simple but powerful tool for any administrator looking to automate the configuration of user printing environments.