Windows printer setup automation by script: Installation

article #1229, updated 879 days ago

Recently there was a case where a Windows Server 2016 print server was set up, and very odd things happened vis a vis one particular large MFP: When any non-admin user changed printer settings on her/his PC only, those settings were replicated to all users on all PCs who had that printer. Microsoft printer deployment was doing it somehow; testing was done both using Print Management and the other GPO print setup method, same result. When manual mappings were used to replace the deployments via Windows Explorer, the symptom went away.

Thus the CMD script below. It can be run as administrator from a PC or remotely via psexec, or a variety of other ways. It installs the driver into the PC, and via scheduled task, any new user that logs into the PC, will have his/her own new mapping to the printer.


ECHO This script will (a) install the driver for a server-shared 
ECHO printer into a PC by UNC, and (b) set up a Scheduled Task 
ECHO to map that printer to any user at logon if they have 
ECHO permissions for that printer.
ECHO It has to be run as administrator, and it requires
ECHO two command line arguments:
ECHO first, a unique, optionally quoted, short name for the printer, 
ECHO used in the task name only; 
ECHO and second, the quoted UNC path of the printer.
ECHO If the unique name already exists in the system, 
ECHO it will be overwritten.  The short name can have no slashes.

:: 1. Get printer printer UNC path via command line.
:: 2. Install printer driver into PC.  This is done by a RUNDLL install of the printer to the administrator user,
:: and is one of two reasons this script has to be run as administrator.
:: Example:  rundll32 printui.dll,PrintUIEntry /q /in /n "\\machine\printer"
:: 3. Set up scheduled task, to be run at user logon and using user creds, to install the printer to the user profile.

:: First we do have to dequote the variables.
Set UniqueName=%~1
Set UNCpath=%~2

:: Step 1.
ECHO Printer to be set up: %UNCpath%
ECHO Using short name: %UniqueName%

:: Step 2.
:: Install the printer into PC as administrator.
ECHO Installing printer as administrator, to get driver in...
rundll32 printui.dll,PrintUIEntry /q /in /n "%UNCpath%"

:: Step 3.
:: Set up the at-login scheduled task...
ECHO Set up the at-login scheduled task...
Set RunString=%windir%\System32\rundll32.exe printui.dll,PrintUIEntry /q /in /n
echo Task will run: %RunString% %UNCpath%
schtasks /Create /F /SC ONLOGON /TN "%1" /TR "%RunString% \"%UNCpath%\""

ECHO Script completed.