use "net Use" to set default printer in batch file

I have to create a batch file to set the users default personal printer to lpt1 in a Windows 7 environment.

Searching the web I found this and this:

net Use lpt1 \\\computername\printer

I know can use %computername% to get the computer name and I can use this to get the default printer name: cscript %Windir%\System32\Printing_Admin_Scripts\en-us\prnmngr.vbs -g

But I do not know how to combine the 2 into one "net use" statement in my batch file.

The end result I would like is net Use lpt1 \\itwin7003\hp laserjet 2035

Thanks in advance!

1 Answer

I have to create a batch file to set the users default personal printer to lpt1

Batch file:

@echo off
setlocal enabledelayedexpansion
for /f "usebackq skip=1 tokens=*" %%i in (`wmic printer where default^="true" get sharename ^| findstr /r /v "^$"`) do ( set _printer=%%i rem remove trailing cr set _printer=!_printer:~0,-1! net use lpt1 "\\%computername%\!_printer!" )
)
endlocal

Command line:

for /f "usebackq skip=1 tokens=*" %i in (`wmic printer where default^="true" get sharename ^| findstr /r /v "^$"`) do @net use lpt1 \\%computername%\%i

Note:

  • net use requires the printer ShareName.

Further Reading

10

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like