Powershell: Get your printers ready

Ready to use functions for your next printer script:

Create Printer

script

function CreatePrinter {
$server = $args[0]
$print = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_Printer").createInstance()
$print.drivername = $args[1]
$print.PortName = $args[2]
$print.Shared = $true
$print.published = $true
$print.Sharename = $args[3]
$print.Location = $args[4]
$print.Comment = $args[5]
$print.DeviceID = $args[6]
$print.Put()
}

usage

createprinter servername drivername portname sharename location comment deviceID

DeviceID and Sharename may be the same. Disable $print.shared and .published if you do not want the printer to be created in AD.
This is great for creating a printer via CSV File.

Create Printer Port

function CreatePrinterPort {
$server =  $args[0]
$port = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()
$port.Name= $args[1]
$port.SNMPEnabled=$false
$port.Protocol=1
$port.HostAddress= $args[2]
$port.Put()
}

usage

CreatePrinterPort servername portname hostaddress

Create the ports first if they do not already exist!

Delete Printer

function DeletePrinter {
    param(
        $Server,
        $Printer
    )
    Get-WmiObject -class "Win32_Printer" -ComputerName $Server -namespace "root\CIMV2" |
    Where-Object {$_.name -eq $printer} | ForEach-Object {
        $_.Delete()
    }
}

Rename Printer


function RenamePrinter {
    param(
        $server,
        $oldName,
        $NewName
    )
    Invoke-WmiMethod -path "Win32_Printer.DeviceID='$oldname'" -name RenamePrinter -ArgumentList "$newname" -ComputerName "$server"
}