📄 Overview
This document provides a guide for the installing printer through PowerShell script, which automates the installation of a specific printer model. The script is designed for idempotency, meaning it can be run multiple times without causing errors, as it checks if components (drivers, ports, printer object) already exist before attempting to create them.
Script for RMM/Intune : https://website-c62a0363.feg.pqz.temporary.site/automated-printer-deployment-script-guide-powershell/
1. Prerequisites and Configuration
Before running the script, ensure the following conditions are met:
| Requirement | Details |
|---|---|
| Permissions | The script must be run with Administrator privileges. |
| Driver File | The initial driver file (in my case CNP60MA64.INF) must be present at the expected staging location: C:\Temp\Driver\CNP60MA64.INF. (This filename is confirmed for the current installation case). |
| PowerShell Version | PowerShell 5.1 or newer (Standard on Windows 10/11) is recommended. |
| Variables | Review the variables at the top of the script and adjust them if the printer’s configuration changes. |
Script Variables
The following variables are defined at the start of the script:
| Variable | Value | Purpose |
|---|---|---|
$PrinterIP | "192.168.15.25" | The target IP address of the network printer. |
$PortName | "IP_192.168.15.25" | The name the TCP/IP port will be assigned. |
$DriverName | "HP Generic Plus PCL6" | The exact name of the printer driver to be used. (Must match the INF file) |
$InfFileName | "CNP60MA64.INF" | The name of the driver installation file. |
$InitialInf | "C:\Temp\Driver\CNP60MA64.INF" | The temporary path where the driver is staged for Pnputil. |
$PrinterName | "HR PRINTER" | The visible name the printer will have in Windows. |
Determining the Exact Driver Name
The $DriverName variable (currently set to "HP Generic Plus PCL6") is case-sensitive and must exactly match the descriptive name defined inside the driver’s .INF file. If you are using a different driver, you must verify this value.
To find the correct name:
- Open the
.INFfile: Use a standard text editor (like Notepad or VS Code) to open theCNP60MA64.INFfile. - Locate the Strings Section: Scroll to the bottom of the file and locate the
[Strings]section, or search for the[Manufacturer]section. - Find the Display Name: Look for a line that contains the human-readable, quoted name of the driver. This is typically the name that appears in the “Add Printer” wizard.
For example, you might see an entry that looks like this:
The exact quoted text (e.g., "HP Generic Plus PCL6") is the value required for the $DriverName variable.
2. Script Logic and Flow
The script follows a sequential, error-checked workflow to ensure reliable installation.
Step 1: Install Driver to Driver Store (Pnputil)
- Command:
Pnputil /add-driver $InitialInf /install - Purpose: This command stages and installs the driver files into the Windows Driver Store.
- Robustness: The script checks the
$LASTEXITCODEfor success or failure, providing a warning but continuing execution, as the driver might already be installed.
Step 2: Dynamically Find Driver Path
- Command:
Get-ChildItem -Path C:\Windows\System32\DriverStore\FileRepository -Filter "*$InfFileName" -Recurse - Purpose: The path created by Windows in the
FileRepositoryis unique and contains an appended hash. This step dynamically searches for the subfolder containing$InfFileNameand retrieves its full path. - Importance: This dynamic search ensures the script works correctly across different Windows machines, as it bypasses the need for a hardcoded, unique path.
Step 3: Add Printer Driver (Add-PrinterDriver)
- Command:
Add-PrinterDriver -Name $DriverName -InfPath $InfPath - Purpose: Registers the installed driver with the Windows Print Spooler service, making it available to the
Add-Printercmdlet. - Robustness: Uses a
try/catchblock to check if the driver already exists. If it does, a success message is displayed, and the script continues.
Step 4: Check and Create Printer Port
- Port Check:
Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue - Port Creation:
Add-PrinterPort -Name $PortName -PrinterHostAddress $PrinterIP - Purpose: The script first checks if a TCP/IP port with the specified
$PortName(e.g.,IP_192.168.15.25) exists. - Robustness: If the port is found, port creation is skipped. If not found, a new port is created. This prevents an error message when re-running the script.
Step 5: Install the Printer Object
- Command:
Add-Printer -DriverName $DriverName -Name $PrinterName -PortName $PortName - Purpose: Creates the final printer object visible in Windows, linking the driver and the port.
- Robustness: Uses a
try/catchblock to handle the scenario where the printer object (HR PRINTER) already exists, preventing a failure and allowing the script to complete gracefully.
Step 6: Verification
- Command:
Get-Printer | Select-Object Name, DriverName, PortName | Format-Table -AutoSize - Purpose: Displays the name, driver, and port for all installed printers, allowing the user to quickly verify the successful installation of the target printer.




