Automated Printer Deployment Script Guide

Automated Printer Deployment Script Guide For RMM, Intune (PowerShell)

📄 Overview

This PowerShell script is designed to automate the installation of a network printer through powershell Script, This script can be use in RMM tools, Intune. It handles the entire process, including downloading the required driver package, extracting it, staging the driver using Pnputil, and finally configuring the printer port, driver, and logical printer object using PowerShell cmdlets (Add-PrinterDriver, Add-PrinterPort, Add-Printer). This is part of the Automated Printer Deployment Script Guide.

It is structured for clear configuration and robust error handling at each stage.

This section details the Automated Printer Deployment Script Guide to ensure seamless installation and management of network printers.

Script for local installation : Automated Printer Installation Script Documentation (Run Locally)

⚙️ Configuration Variables

The script relies on several variables for configuration. You must verify and update these values to match your specific printer and network environment.

VariableExample ValueDescription
$Url1https://.../setup.zipThe direct URL to the compressed driver file (must be a .zip file). Upload file to any publicly accessible location, such as an Azure Blob.
$ZipFileC:\Windows\Temp\setup.zipThe local path where the driver ZIP file will be temporarily saved.
$DriverFolderC:\Windows\Temp\DriverThe sub-folder where the ZIP contents will be extracted.
$InfFile$DriverFolder\CNP60MA64.INFThe exact path and name of the primary INF file within the extracted driver package. This is critical for Pnputil.
$PrinterIP192.168.1.20The static IP address of the physical network printer.
$DriverName"HP Generic PCL6"The exact name of the printer model or driver as listed inside the INF file.
$PortName"IP_192.168.1.20"The name for the newly created standard TCP/IP port. It’s automatically constructed from the IP address.
$PrinterNameHR Printer"The friendly name you want the printer to be listed under in the Windows control panel.

🛠️ Step-by-Step Breakdown

The script executes in six main stages:

1. Download and Extract Driver

  • Uses Invoke-WebRequest to securely download the driver ZIP file from the specified $Url1 to the $ZipFile path.
  • Uses Expand-Archive to extract the contents of the ZIP into the C:\Windows\Temp directory, ensuring the target INF file is available at the $InfFile path.
  • Includes a try/catch block to handle network or file extraction errors.

2. Add Driver using Pnputil

  • The script uses Pnputil /add-driver $InfFile /install to stage the driver package into the Windows DriverStore and attempt installation.
    • Pnputil is a built-in Windows utility for managing driver packages.

3. Find the DriverStore Path

  • This is a crucial step because Add-PrinterDriver needs the final, unique path of the INF file after Windows has staged it.
  • It uses Get-ChildItem to search the C:\Windows\System32\DriverStore\FileRepository directory for the specific driver file name (CNP60MA64.INF).
  • The result is stored in the $DriverStorePath variable. If the path isn’t found, the script terminates, as the printer components cannot be added without it.

4. Add Printer Components

  • This section uses the built-in PowerShell Printer Management cmdlets:
    1. Add-PrinterDriver: Installs the driver in the print subsystem, referencing the $DriverName and the confirmed $DriverStorePath.
    2. Add-PrinterPort: Creates a new standard TCP/IP port named $PortName pointing to the printer’s $PrinterIP.
    3. Add-Printer: Creates the final logical printer object in Windows, linking the $DriverName and $PortName under the user-friendly $PrinterName.

5. Verification

  • The script concludes by running Get-Printer to display the properties of all installed printers, confirming that the new printer is listed with the correct driver and port names.

Powershell : List installed printers
# 1. Variables and Setup
$Url1 = 'https://public.com/Drivers/setup.zip'
$ZipFile = "C:\Windows\Temp\setup.zip"
$DriverFolder = "C:\Windows\Temp\Driver"
$InfFile = "$DriverFolder\CNP60MA64.INF"
$PrinterIP = "192.168.1.20"
$DriverName = "HP Generic  PCL6" # Must match the name inside the INF file
$PortName = "IP_$($PrinterIP)"
$PrinterName = "HR Printer"

# 2. Download and Extract Driver
Write-Host "Downloading and extracting driver package..."
try {
    Invoke-WebRequest -Uri $Url1 -OutFile $ZipFile -ErrorAction Stop
    Expand-Archive -Path $ZipFile -DestinationPath "C:\Windows\Temp" -Force
} catch {
    Write-Error "Error during download or extraction: $($_.Exception.Message)"
    exit 1
}

# 3. Add Driver using Pnputil
Write-Host "Adding driver using Pnputil..."
try {
    # The Pnputil command should point to the folder or the specific INF file.
    # Using /add-driver and the INF path:
    Pnputil /add-driver $InfFile /install
} catch {
    Write-Error "Error running Pnputil: $($_.Exception.Message)"
    # Continue as the error might be non-fatal if the driver was already staged
}

# 4. Find the DriverStore Path
Write-Host "Searching for the driver's path in DriverStore..."
# Get the full path of the INF file in the DriverStore after it's been staged/installed.
# This searches subdirectories for the specific INF file name.
$DriverStorePath = Get-ChildItem -Path "C:\Windows\System32\DriverStore\FileRepository" -Recurse -Filter "CNP60MA64.INF" |
    Where-Object { $_.PSIsContainer -eq $false } |
    Select-Object -ExpandProperty FullName -First 1

if (-not $DriverStorePath) {
    Write-Error "Could not find 'CNP60MA64.INF' in the DriverStore after Pnputil. Cannot proceed with Add-PrinterDriver."
    exit 1
}

Write-Host "Found DriverStore path: $DriverStorePath"

# 5. Add Printer Components
Write-Host "Adding printer driver, port, and printer..."

try {
    # Add Printer Driver using the discovered DriverStore path
    Add-PrinterDriver -Name $DriverName -InfPath $DriverStorePath -ErrorAction Stop

    # Add Standard TCP/IP Port
    Add-PrinterPort -Name $PortName -PrinterHostAddress $PrinterIP -ErrorAction Stop

    # Add Printer
    Add-Printer -DriverName $DriverName -Name $PrinterName -PortName $PortName -ErrorAction Stop
} catch {
    Write-Error "Error during printer configuration: $($_.Exception.Message)"
    exit 1
}

# 6. Verification
Write-Host "Printer installation complete. Verifying..."
Get-Printer | Select-Object Name, DriverName, PortName