Adding printers with Microsoft Endpoint Manager using PowerShell

For one of our customers I made a script which adds printers to their Endpoint Manager clients using a CSV-file for data like FQDN of the printer, model and location of the printer. In this blogpost I will show you how to do that with a few example printers.

Files needed for the Win32 package

CSV File

This is an example file in which I add four printers, save this as printers.csv and make sure the drivers are available on the system. (Check the Drivers tab inside Print server Properties)

"Name","DriverName","PortName","Comment","Location"
"Contoso-MFP-General","TOSHIBA Universal Printer 2","general.contoso.local","Contoso","Main Office"
"Contoso-MFP-HR","TOSHIBA Universal Printer 2","hr.contoso.local","Contoso","HR"
"Contoso-PRT-IT","HP Universal Printing PCL 6 (v7.0.0)","it-prt.contoso.local","Contoso","IT"
"Contoso-MFP-Legal","TOSHIBA Universal Printer 2","legal.contoso.local","Contoso","Legal"

PowerShell install script

Below is the script, save it as add_printers.ps1. For the printer options I choose Black and two sided on long edge as default options. (You can use other options, see Link for more)

#Read printers.csv as input
$Printers = Get-Content .\printers.csv | ConvertFrom-Csv

#Loop through all printers in the csv-file and add the Printer port, the printer and associate it with the port and set the color options to 0 which is black and white (1 = automatic and 2 = color)
foreach ($printer in $printers) {
    #Use Splatting for the options
    $PrinterPortOptions = @{
        Name               = $Printer.Name
        PrinterHostAddress = $Printer.PortName
        PortNumber         = '9100'
    }

    $PrinterAddOptions = @{
        Comment    = $Printer.Comment
        DriverName = $Printer.DriverName
        Location   = $Printer.Location
        Name       = $Printer.Name
        PortName   = $Printer.Name
    }

    $PrinterConfigOptions = @{
        Color       = 0
        DuplexingMode = 'TwoSidedLongEdge'
        PrinterName = $Printer.Name
    }

    #Add Printerport, printer and configure it with the options required
    Add-PrinterPort @PrinterPortOptions
    Add-Printer @PrinterAddOptions
    Set-PrintConfiguration @PrinterConfigOptions
}

#Add check file to c:\programdata for detection
New-Item -Path C:\ProgramData\Contoso\Printers.txt -ItemType File -Confirm:$false -Force:$true

PowerShell uninstall script

Below is the uninstall script which removes all printers listed in the CSV-file, save it as remove_printers.ps1.

#Read printers.csv as input
$Printers = Get-Content .\printers.csv | ConvertFrom-Csv

#Loop through all printers in the csv-file and remove the printers listed
foreach ($printer in $printers) {
    #Use Splatting for the options
    
    $PrinterRemoveOptions = @{
        Confirm = $false
        Name    = $Printer.Name
    }

    $PrinterRemoveOptions = @{
        Confirm = $false
        Name    = $Printer.Name
    }

    #Remove printers and their ports
    Remove-Printer @PrinterRemoveOptions
    Start-Sleep -Seconds 60
    Remove-PrinterPort @PrinterRemoveOptions
}

#Remove check file
Remove-Item -Path C:\ProgramData\Contoso\Printers.txt -Confirm:$false -Force:$true

Endpoint Manager install/uninstall script

Below are the install and uninstall command-lines for the Win32 package.

Install

powershell.exe -executionpolicy bypass -file .\add_printers.ps1

Uninstall

powershell.exe -executionpolicy bypass -file .\remove_printers.ps1

Add to Endpoint Manager

Put the files in a directory, create a Intunewin package from it, add to Endpoint Manager and use a File detection on C:\ProgramData\Contoso\Printers.txt.

The result

After deploying, printers should be visible to the users and they can choose their own Default Printer.

Download the script(s) from GitHub here

Leave a Reply

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