Advertisement
Guest User

How to Add Printers with PowerShell

a guest
Jul 30th, 2014
2,956
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ####################################################
  2. # Change these values to the appropriate values in your environment
  3.  
  4. $PrinterIP = "10.10.10.10"
  5. $PrinterPort = "9100"
  6. $PrinterPortName = "IP_" + $PrinterIP
  7. $DriverName = "KONICA MINOLTA bizhub C35P PS"
  8. $DriverPath = "\\UNC_Path\To\My\Drivers"
  9. $DriverInf = "\\UNC_Path\To\My\Drivers\KOBJQA__.inf"
  10. $PrinterCaption = "Konica Minolta C35P"
  11. ####################################################
  12.  
  13. ### ComputerList Option 1 ###
  14. # $ComputerList = @("lana", "lisaburger")
  15.  
  16. ### ComputerList Option 2 ###
  17. # $ComputerList = @()
  18. # Import-Csv "C:\Temp\ComputersThatNeedPrinters.csv" | `
  19. # % {$ComputerList += $_.Computer}
  20.  
  21. Function CreatePrinterPort {
  22. param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
  23. $wmi = [wmiclass]"\\$ComputerName\root\cimv2:win32_tcpipPrinterPort"
  24. $wmi.psbase.scope.options.enablePrivileges = $true
  25. $Port = $wmi.createInstance()
  26. $Port.name = $PrinterPortName
  27. $Port.hostAddress = $PrinterIP
  28. $Port.portNumber = $PrinterPort
  29. $Port.SNMPEnabled = $false
  30. $Port.Protocol = 1
  31. $Port.put()
  32. }
  33.  
  34. Function InstallPrinterDriver {
  35. Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
  36. $wmi = [wmiclass]"\\$ComputerName\Root\cimv2:Win32_PrinterDriver"
  37. $wmi.psbase.scope.options.enablePrivileges = $true
  38. $wmi.psbase.Scope.Options.Impersonation = `
  39. [System.Management.ImpersonationLevel]::Impersonate
  40. $Driver = $wmi.CreateInstance()
  41. $Driver.Name = $DriverName
  42. $Driver.DriverPath = $DriverPath
  43. $Driver.InfName = $DriverInf
  44. $wmi.AddPrinterDriver($Driver)
  45. $wmi.Put()
  46. }
  47.  
  48. Function CreatePrinter {
  49. param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
  50. $Printer = ([WMIClass]"\\$ComputerName\Root\cimv2:Win32_Printer")
  51. $Printer.CreateInstance()
  52. $Printer.Caption = $PrinterCaption
  53. $Printer.DriverName = $DriverName
  54. $Printer.PortName = $PrinterPortName
  55. $Printer.DeviceID = $PrinterCaption
  56. $Printer.Put()
  57. }
  58.  
  59. foreach ($computer in $ComputerList) {
  60. CreatePrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort `
  61. -PrinterPortName $PrinterPortName -ComputerName $computer
  62. InstallPrinterDriver -DriverName $DriverName -DriverPath `
  63. $DriverPath -DriverInf $DriverInf -ComputerName $computer
  64. CreatePrinter -PrinterPortName $PrinterPortName -DriverName `
  65. $DriverName -PrinterCaption $PrinterCaption -ComputerName $computer
  66. }
  67. ####################################################
Advertisement
RAW Paste Data Copied
Advertisement