PoshCode Logo PowerShell Code Repository

Create-Printers.ps1 by Jan Egil Ring 7 years ago
View followups from cccccc, Zimmer, Richard, Richard and Andreas | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1462"></script>download | new post

Simple script to bulk-create printers on a print-server. Printers are imported from a csv-file.

  1. ###########################################################################"
  2. #
  3. # NAME: Create-Printers.ps1
  4. #
  5. # AUTHOR: Jan Egil Ring
  6. # EMAIL: jan.egil.ring@powershell.no
  7. # BLOG: http://blog.powershell.no
  8. #
  9. # COMMENT: Simple script to bulk-create printers on a print-server. Printers are imported from a csv-file.
  10. #          Running the script from Windows Server 2003 returns an access denied error, possibly due to the impersonation-model in Windows Server 2003.
  11. #          Created and tested from Windows Server 2008 against a remote Windows Server 2003 print-server.
  12. #          Should work from Windows Vista, Windows 7, Windows Server 2008 and Windows Server 2008 R2 against remote print-servers (2000/2003/2008/2008 R2)
  13. #
  14. # You have a royalty-free right to use, modify, reproduce, and
  15. # distribute this script file in any way you find useful, provided that
  16. # you agree that the creator, owner above has no warranty, obligations,
  17. # or liability for such use.
  18. #
  19. # VERSION HISTORY:
  20. # 1.0 07.11.2009 - Initial release
  21. #
  22. ###########################################################################"
  23.  
  24.  
  25.  
  26. function CreatePrinter {
  27. $server = $args[0]
  28. $print = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_Printer").createInstance()
  29. $print.drivername = $args[1]
  30. $print.PortName = $args[2]
  31. $print.Shared = $true
  32. $print.Sharename = $args[3]
  33. $print.Location = $args[4]
  34. $print.Comment = $args[5]
  35. $print.DeviceID = $args[6]
  36. $print.Put()
  37. }
  38.  
  39. function CreatePrinterPort {
  40. $server =  $args[0]
  41. $port = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()
  42. $port.Name= $args[1]
  43. $port.SNMPEnabled=$false
  44. $port.Protocol=1
  45. $port.HostAddress= $args[2]
  46. $port.Put()
  47. }
  48.  
  49. $printers = Import-Csv c:\printers.csv
  50.  
  51. foreach ($printer in $printers) {
  52. CreatePrinterPort $printer.Printserver $printer.Portname $printer.IPAddress
  53. CreatePrinter $printer.Printserver $printer.Driver $printer.Portname $printer.Sharename $printer.Location $printer.Comment $printer.Printername
  54. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me