Programmatically add printers to a Windows server

We install servers for organizations that sometimes have hundreds of printers. Gathering information on those printers and then creating new ones on the new print server can be tedious at best. To get a list of printers, you have to run the following script on the local print server. If you have more than one print server, you will need to run this on each one and compile the information manually.

<#
Purpose: To get a list of printers, printer ports, share name, comments, location and driver names from a Windows server.
Author: Michael Kenning (mjkenning@gmail.com)
Date: 23 OCT 2014
Version: 1.0 (release)

Usage: ./getprinters.ps1 ./FILENAME.CSV
#>

$printserver = "localhost"
Get-WMIObject -class Win32_Printer -computer $printserver | Select Name,DriverName,PortName,ShareName,Comment,Location | Export-CSV -path '.\printers.csv'

When you’ve compiled your information from this script into a CSV file (you’ll need to add the IP Address), you are ready to run the script to add the new printers. But first, you will need to verify that the driver names are the same!

<#
Purpose: To create printers, printer ports, and add drivers to a Windows 2012/2012R2 server.
Author: Michael Kenning (mjkenning@gmail.com)
Date: 23 JUL 2015
Version: 1.32 (release)

Usage: ./addprinters.ps1 ./FILENAME.CSV
#>

param (
	[Parameter(Mandatory=$true,Position=0)]
	[ValidateNotNullOrEmpty()]
	[String]
	$path
)

$printers_csv = Import-CSV $path
$drivers = get-printerdriver | select -expand name
$ports = get-printerport | select -expand name
$instPrinters = get-printer | select -expand name


foreach ($printer in $printers_csv) {
	
	$portInstalled = $null
	$driverInstalled = $null
	$printerInstalled = $null
	$printerName = $printer.Name
	$driverName = $printer.driverName
	$portName = $printer.portName
	$shareName = $printer.shareName
	$location = $printer.location
	$comment = $printer.comment
	$ipAddress = $printer.ipAddress

	write-host "Checking" $printerName "..."
	foreach ($instPrinter in $instPrinters) {
		if ($instPrinter -eq $printerName) {
			write-host $printerName "already installed"
			$printerInstalled = $true
			break
		}
	}

	foreach ($driver in $drivers) {
		if ($driverName	-eq $driver) {
			$driverInstalled = $true
			write-host $driver "already installed"
			break
		}
		else {$driverInstalled = $false}

	}
	if ($driverInstalled -eq $false) {
		Add-PrinterDriver -Name $driverName -Confirm:$false
		write-host "Driver" $driverName "installed."
		$driverInstalled = 1
	}
	foreach ($port in $ports) {
		if ($portName -eq $port) {
			$portInstalled = $true
			write-host $port "is already created."
			break
		}
		else {$portInstalled = $false}
	}
	if ($portInstalled -ne $true) {
		Add-PrinterPort -Name $portName -PrinterHostAddress $ipAddress -Confirm:$false
		write-host "Portname" $portName "created."
		$portInstalled = $true
	}

	if (($driverInstalled) -and ($portInstalled) -and ($printerInstalled -ne $true)) {
		Add-Printer -Name $printerName -DriverName $driverName -PortName $portName -Comment $comment -Location $location -Shared -Sharename $shareName -Published -Confirm:$false
	}
}
Advertisements
Tagged with: , ,
Posted in Powershell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: