@echo off

IF %1.==. GOTO USAGE
IF %2.==. GOTO USAGE
IF %3.==. GOTO USAGE
IF %4.==. GOTO USAGE

set DRIVERPATH=%1%
set DRIVERNAME=%2%
set PRINTERIP=%3%
set PRINTERNAME=%4%

set PORTNAME=IP_%PRINTERIP%

if NOT EXIST %DRIVERPATH% (
  echo ERROR: %DRIVERPATH% not found!
  exit /b
)

rem install the inf files ...
PNPUtil.exe /add-driver %DRIVERPATH%\*.inf /install

rem add the printer driver ...
powershell -Command "Add-PrinterDriver -Name '%DRIVERNAME%' -Verbose"

rem add the printer port ...
powershell -Command ^
"$checkPortExists = Get-Printerport -Name %PORTNAME% -ErrorAction SilentlyContinue ; ^
if (-not $checkPortExists) { ^
  Add-PrinterPort -Name %PORTNAME% -PrinterHostAddress %PRINTERIP% -Verbose ^
} else { ^
  Write-Host 'Printerport %PORTNAME% already installed' ^
}"

rem add the printer ...
powershell -Command ^
"$checkPrinterExists = Get-Printer -Name '%PRINTERNAME%' -ErrorAction SilentlyContinue ; ^
if (-not $checkPrinterExists) { ^
  Add-Printer -Name '%PRINTERNAME%' -DriverName '%DRIVERNAME%' -PortName %PORTNAME% -Verbose ^
} else { ^
  Write-Host 'Printer %PRINTERNAME% already installed' ^
}"
echo.

echo setting default printer to [ %PRINTERNAME% ]
powershell -Command ^
"Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows' ^
  -Name LegacyDefaultPrinterMode -Value 1 -Force"

powershell -Command "$Printer = Get-CimInstance -Class Win32_Printer -Filter \"Name='%PRINTERNAME%'\"" ; ^
  Write-Host $Printer ; ^
  Invoke-CimMethod -InputObject $Printer -MethodName SetDefaultPrinter"

echo disabling DUPLEX mode ...
powershell -Command "Set-PrintConfiguration -PrinterName '%PRINTERNAME%' -DuplexingMode OneSided -Verbose"

GOTO END

:USAGE
echo "usage: %0 <DRIVERPATH> <DRIVERNAME> <PRINTERIP> <PRINTERNAME>"

:END
