Friday, 25 July 2014

Powershell script to add printers from CSV and adjust driver

If you find yourself needing to do a printer migration but cant/don't want to re-install the old drivers (e.g. a cross architecture migration), you can import your printers from a .csv and change the driver as needed:
 
CSV Format:


Printserver,drivername,PortName,IPAddress,Sharename,Location,Comment,Printername
XXX,HP Test,IP_x.x.x.x,x.x.x.x,printername,location test,comment test,printername

Script:


function CreatePrinter {

$server = $args[0]

$print = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_Printer").createInstance()

$print.drivername = $args[1]

$print.PortName = $args[2]

$print.Shared = $true

$print.Sharename = $args[3]

$print.Location = $args[4]

$print.Comment = $args[5]

$print.DeviceID = $args[6]

$print.Put()

}

function CreatePrinterPort {

$server = $args[0]

$port = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()

$port.Name= $args[1]

$port.SNMPEnabled=$false

$port.Protocol=1

$port.HostAddress= $args[2]

$port.Put()

}

 

 

$printers = Import-Csv c:\printers.csv

foreach ($printer in $printers) {

CreatePrinterPort $printer.Printserver $printer.Portname $printer.IPAddress

CreatePrinter $printer.Printserver $printer.drivername $printer.Portname $printer.Sharename $printer.Location $printer.Comment $printer.Printername

}

4 comments:

  1. Many thanks for this brilliant post! Many points have extremely useful. Hopefully you'll continue sharing your knowledge around.
    drukarka 3d

    ReplyDelete
    Replies
    1. Hi Peter
      Can you please share the screen shot of how csv file fields are filled.

      Delete
  2. $ printers = Import-Csv c:\users\Michel.aras\desktop\printers.csv

    does not work

    ReplyDelete
  3. This is a fantastic post! The PowerShell script for adding printers from a CSV file is a real time-saver, especially for administrators managing multiple devices in large environments. Automating printer deployment can significantly reduce errors and improve efficiency, so I appreciate the detailed explanation and script example you've provided.

    One question: Do you have any recommendations for handling scenarios where a printer driver isn’t pre-installed on the machine? It would be great to hear your insights on incorporating driver installation into the script.

    Thank you for sharing such practical and valuable content!

    Epicforce Tech

    ReplyDelete