Using a PowerShell Script to Add a Printer Driver by IP

Published by Nyau Wai Hoe - Updated on

PowerShell is a powerful scripting language that comes with Microsoft Windows. It provides system administrators and power-users with the ability to automate routine tasks. In this article, we will walk you through a PowerShell script that adds a TCP/IP printer to a Windows 11 or 10 computer.

For this task, we will use a generic printer driver in our examples. You should replace it with the appropriate driver that matches the printer you are attempting to add. This script will require the INF file for the driver. INF files contain the information that Windows needs to install hardware components. We’ll explain how to locate and extract this file from the printer driver executable.

Using a PowerShell Script to Add a Printer Driver by IP

Note: This script is designed for Windows 8 and newer operating systems, as the “add-printer” cmdlets are not available on Windows 7.

Also see: How to Reinstall Printer Driver on Windows 11

Obtaining the printer driver INF file

Before we jump into the script, we first need to have the INF file for our printer driver ready. An INF file is a text file that gives the setup program the information needed to install your hardware. You can extract this file from the printer driver executable.

If the driver package is a .exe file, you will need a tool like 7-Zip to extract its contents and find the required INF file. Follow the steps below to do this:

  1. Download and install 7-Zip.
  2. Use 7-Zip to open and extract the printer driver .exe file.How to Extract Printer INF in Windows 11
  3. Open PowerShell and change directory to the extracted folder.
  4. Run this command to find and copy the INF file to a location of your choice:
    get-childitem *.inf* |copy-item -destination "C:\desiredfolder"

    Remember to replace C:\desiredfolder with the directory where you want to store the INF files.
    Find Printer INF driver in Windows 11

Useful resource: How to See What Drivers Are Installed in Windows 11

PowerShell script to add a printer driver

Now that you have the necessary INF file, here’s the PowerShell script you can use to add a TCP/IP printer to a local computer:

$driverName = "Your Printer Driver"
$printerIP = "x.x.x.x"
$deviceName = "Your Printer Name"
$pauseInterval = "3"

Invoke-Command {pnputil.exe -a "C:\path_to_driver\printer.inf" }

Add-PrinterDriver -Name $driverName

Start-Sleep $pauseInterval

Add-PrinterPort -Name $printerIP -PrinterHostAddress $printerIP

Start-Sleep $pauseInterval

Add-Printer -DriverName $driverName -Name $deviceName -PortName $printerIP

Start-Sleep $pauseInterval

get-printer |Out-Printer -Name $deviceName

PowerShell script to add a printer driver

This script is composed of six main sections. Let’s dissect this script into its these sections to better understand its operations.

Section 1: Defining variables

$driverName = "Your Printer Driver"
$printerIP = "x.x.x.x"
$deviceName = "Your Printer Name"
$pauseInterval = "3"

These four variables at the top of the script are essential for the functioning of the script. Here’s what they each mean:

  • $driverName: The name of the printer driver to be installed.
  • $printerIP: The IP address of the printer. Replace “x.x.x.x” with your printer’s actual IP address.
  • $deviceName: The name that will be assigned to the printer on your system.
  • $pauseInterval: The number of seconds the script should pause between commands. This is necessary because some commands may need a few seconds to complete before the next command can be run.

Add a printer by IP using PowerShell

Section 2: Importing the printer driver

Invoke-Command {pnputil.exe -a "C:\path_to_driver\printer.inf" }

This command uses pnputil.exe to import the INF file of the printer driver into the Windows driver store. pnputil.exe is a command-line utility that you can use to manage the driver store in Windows. You need to replace C:\path_to_driver\printer.inf with the actual path to your INF file.

Install Printer INF driver Windows PowerShell

Section 3: Installing the printer driver

Add-PrinterDriver -Name $driverName
Start-Sleep $pauseInterval

The Add-PrinterDriver cmdlet installs the printer driver on your computer. It uses the $driverName variable defined earlier. The Start-Sleep cmdlet following it pauses the script for the number of seconds defined by $pauseInterval.

Section 4: Creating the printer port

Add-PrinterPort -Name $printerIP -PrinterHostAddress $printerIP
Start-Sleep $pauseInterval

The Add-PrinterPort cmdlet creates a new TCP/IP printer port with the name and host address being the printer’s IP address, as defined by the $printerIP variable. After this, Start-Sleep pauses the script for $pauseInterval seconds.

Section 5: Adding the printer

Add-Printer -DriverName $driverName -Name $deviceName -PortName $printerIP
Start-Sleep $pauseInterval

The Add-Printer cmdlet adds the printer to the system. This command needs the driver name, the printer name, and the port name. It takes these values from the variables we defined at the start of the script. After the printer is added, the script is paused again for the duration specified in $pauseInterval.

Section 6: Verifying the printer installation

get-printer |Out-Printer -Name $deviceName

In this final section, the get-printer cmdlet retrieves a list of all printers installed on the system. This list is then sent to the new printer using Out-Printer. This is a proof-of-concept to confirm that the newly installed printer is working correctly.

Related guide: How to AirPrint From iOS to Windows 11/10 Shared Printer

Using the PowerShell script to add a printer to a remote computer

While the script we’ve discussed is primarily intended for local use, it can be easily adapted to add a printer driver to a remote computer. This becomes incredibly useful when managing multiple workstations or dealing with a server-client setup. Here’s how you can modify the script for remote use:

  1. Ensure the INF File is Accessible Remotely: The INF file must be present on the remote computer where you wish to install the printer. You can achieve this by manually copying the file to the remote system or using a file transfer cmdlet in PowerShell such as Copy-Item.
  2. Modify the Invoke-Command: The Invoke-Command cmdlet allows for remote execution when used with the -ComputerName parameter. Modify the command in the script as follows:
    Invoke-Command -ComputerName "RemotePC" {pnputil.exe -a "C:\path_to_driver\printer.inf"}

    Replace “RemotePC” with the name or IP address of the remote computer. Ensure that the path to the INF file is correct and accessible on the remote machine.

  3. Run the Script: Once the modifications are made, you can run the script as usual. However, ensure that you have the necessary permissions to execute commands remotely. Windows remoting should be enabled and properly configured on the remote computer.

PowerShell add printer to remote computer

Running the script

To execute the script, open PowerShell as an administrator, paste the script into the console, and hit Enter. Be sure to replace the placeholders in the script with the values specific to your printer.

The Start-Sleep cmdlets interspersed between operations allow each operation to complete before the next one begins. This is necessary as certain operations might take a few seconds to finish, and running the next operation prematurely might cause errors.

This guide has used a universal printer driver for demonstration, but you should replace it with the driver that matches your specific printer model.

In conclusion, this PowerShell script provides a versatile and efficient way to add a printer to your machine. With the correct INF file, it works with any printer and can even be adjusted to work on remote machines.


Nyau Wai Hoe
Nyau Wai Hoe is the Founder and Chief Editor of WindowsDigitals.com. With a degree in software engineering and over 12 years of experience in the tech support industry, Nyau has established himself as an expert in the field, with a primary focus on the Microsoft Windows operating system. As a tech enthusiast, he loves exploring new technologies and leveraging them to solve real-life problems.

Share via
Copy link