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
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 printerShareName
.
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- for /f - Loop command against the results of another command.
- net - The NET Command is used to manage network resources.
- wmic - Windows Management Instrumentation Command.