Adding printers with Microsoft Endpoint Manager using PowerShell

For one of our customers, I made a script that adds printers to their Endpoint Manager clients using a CSV file for data like the FQDN of the printer, model, and location. In this blog post, 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 the 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       = $False
        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, saves 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 an Intunewin package, add it 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, who can choose their Default Printer.

Download the script(s) from GitHub here

2 thoughts on “Adding printers with Microsoft Endpoint Manager using PowerShell

  1. Hi Harm,

    Thank you for your blog, it did help us a lot. I am in trouble adding the printer with B&W as default. But the Twoside is working. I am trying to add a FujiFilm Xerox printer, can you let me know if the cmdlets for a FujiFilm Xerox printer please? I am thinking it could be a different cmdlets for Color = 0 for a a FujiFilm Xerox printer. Thank you.

    $PrinterConfigOptions = @{
    Color = 0
    DuplexingMode = ‘TwoSidedLongEdge’
    PrinterName = $Printer.Name

    • I made a mistake in the PrinterConfigOptions, it should be $true or $false for the Color option! 🙁 I changed the blog post and GitHub to reflect that, did a test just now and Black & White is now selected for my test printers. Can you test this too?

      Note: Some printers have an auto Color / Black and white setting, doesn’t work for those as far as I can see

Leave a Reply

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