0

I have printer RDS 2012 issues and using Group policy to create network printers because printing reliablity is woeful on RDS 2012 and been a terminal services box net spooler stops and start

Group Policy Preferences is alo a bit unreliable

I would like the ability to recreate the printers using Powershell

Get-Printer  | remove-printer   get rid of them o.k

but how do I recreate printers.

user3265817
  • 99
  • 1
  • 13

2 Answers2

0

Before you remove the printers safe the information about them in a variable. After the removing step you can add them with the cmdlet Add-printer.

guiwhatsthat
  • 2,349
  • 1
  • 12
  • 24
0

On PowerShell 3 or less, You can use WMI,

You need to Create a Printer IP Port(win32_tcpipPrinterPort Class), add Driver(Win32_PrinterDriver Class), then create a printer(Win32_Printer Class),

You can use this helper functions, for each of the tasks:

Function CreatePrinterPort {
Param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
$wmi = [wmiclass]"\\$ComputerName\root\cimv2:win32_tcpipPrinterPort"
$wmi.psbase.scope.options.enablePrivileges = $true
$Port = $wmi.createInstance()
$Port.name = $PrinterPortName
$Port.hostAddress = $PrinterIP
$Port.portNumber = $PrinterPort
$Port.SNMPEnabled = $false
$Port.Protocol = 1
$Port.put()
}

Function InstallPrinterDriver {
Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
$wmi = [wmiclass]"\\$ComputerName\Root\cimv2:Win32_PrinterDriver"
$wmi.psbase.scope.options.enablePrivileges = $true
$wmi.psbase.Scope.Options.Impersonation = [System.Management.ImpersonationLevel]::Impersonate
$Driver = $wmi.CreateInstance()
$Driver.Name = $DriverName
$Driver.DriverPath = $DriverPath
$Driver.InfName = $DriverInf
$wmi.AddPrinterDriver($Driver)
$wmi.Put()
}

Function CreatePrinter 
{
param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
$wmi = ([WMIClass]"\\$ComputerName\Root\cimv2:Win32_Printer")
$Printer = $wmi.CreateInstance()
$Printer.Caption = $PrinterCaption
$Printer.DriverName = $DriverName
$Printer.PortName = $PrinterPortName
$Printer.DeviceID = $PrinterCaption
$Printer.Put()
}

Example use:

Create Port:

CreatePrinterPort -PrinterIP 192.168.100.100 -PrinterPort 9100 -PrinterPortName 192.168.100.100 -ComputerName $Computer

Install Driver:

InstallPrinterDriver -ComputerName $Computer -DriverName "Xerox Phaser 3600 PCL 6" -DriverPath "C:\PrinterDrivers\Xerox\x64" -DriverInf "C:\PrinterDrivers\Xerox\x64\sxk2m.inf"

Add Printer:

CreatePrinter -PrinterCaption "Xerox Phaser 3600 PCL 6" -PrinterPortName "192.168.100.100" -DriverName "Xerox Phaser 3600 PCL 6" -ComputerName $Computer

Needless to say, You need Admin Permissions on the target computer...

Good luck

Avshalom
  • 8,657
  • 1
  • 25
  • 43