Powershell – Add Printers and Shares in 2012 R2

In my previous post I installed the print drivers I needed, now I am going to add the printers

Ironically as Windows Server 2003 is stopped being supported by Microsoft… I need to use the setprinter.exe utility from the Windows Server 2003 Resource Kit to set the defaults I want on the printers I create, as Powershell 4 cannot do this…..

Things to note:
All my printers are networked, so they have an IP address assigned
Each printer’s IP address is entered into the DNS e.g
Printer1 = 10.1.1.100
Printer2 = 10.1.1.110

So the Printer(s) I create has the same Printername,sharename and portname. This makes life easy.

So in the powershell below, first I am creating  a list of printers to create.

Then I am looping through a foreach to create the printers

  1.  Adding the TCP port (Add-PrinterPort)
  2. Adding the printer with a specific driver, connected to a specific port and sharing it with the same name (Add-Printer)
  3. Lastly I am using the setprinter.exe to set the specific printer defaults to Black and White ,single sided and A$ paper (Setprinter.exe)

SetPrinter is poorly documented around some of its settings. I struggled with getting the paper size to A4. This post here  helps with explaining the pdevmode options. The DMPAPERSIZE table there lists the paper sizes (funnily enough) but you have to count the rows to get the right(ish) number to enter into dmPageSize=X. So in my case I wanted A4, but which one? Well I tried 8 and it gave me A3, so I tried 9 and it gave me A4.

 

$PrintQueuesToinstall = @(
"Printer1","Printer2"
)
$count = 1
foreach ($item in $PrintQueuesToInstall)
{

Add-PrinterPort -Name $item  -PrinterHostAddress $item 
Write-Host     "######################################################################"
Write-Host     "$count of " $PrintQueuesToIinstall.count "  installing Queue: $item"
Add-Printer -Name $item  -Drivername "RICOH PCL6 UniversalDriver V4.6" -PortName $item -Shared -ShareName $item
c:\temp\setprinter.exe  $item 2 "pdevmode=dmduplex=1,dmcolor=1,dmPageSize=9"
$count++
}

One thought on “Powershell – Add Printers and Shares in 2012 R2

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.