PDQ.com mobilePDQ.com desktop
Support

Using PowerShell to Install Printers

Jordan HammondJordan Hammond
·

Printers are dumb. I have done a study of my own behavior(hereto known as sysadmin) and have concluded that 87.5% of feet kicking tantrums thrown by sysadmins are caused by working with printers. With this information, you could focus on me, a grown man who has child-like fits, or instead, you could concentrate on some neat PowerShell in here that will make setting up these demon devices a little less terrible.

Group Policy First

Before you even look at PowerShell, do you have the option to set this up with Group Policy? If yes, do that. Seriously, you can set up printers to install on devices based on IP range, so if your networking is all tidy, anybody could move a device anywhere in the building, and they will have the closest printer available to them.

PowerShell with a Print Server

Moving past Group Policy, if you have a print server, it is effortless to do in PowerShell. It is a one-liner.

Add-Printer -ConnectionName \\printserver\printername

Nice and easy. The print server will handle all of the steps. Namely, add the driver to the store, install the driver, create the printer port, and finally install the printer. That is a lot to pack into a single command.

Installing with Just PowerShell

Finally, let us take a look at what we need to do when we need to install a printer. It can get a little tricky here, and I will be honest with you, research into this contributed to 87.5%. We will need to manually do the four things we mentioned in the Print Server section. I am assuming that you have gone to the printer vendor and got the drivers you need, and we are just using PowerShell to get them onto machines.

Add Driver to the Store

Microsoft can't have drivers for every device already in the driver store; there is too much. So it is likely that the printer you have will not already be in the driver store. I use pnputil.exe to do this. I can hear your complaints now! "That is not PowerShell," and "How Dare You!!!!!!". I have just found that this works best for me. I will also run it from PowerShell if that helps. The second roadblock was I was unclear if I could use a UNC path with the command. I can honestly tell you I spent 30 minutes trying to find documentation on this and came up empty. I did find a forum post where a person said they could not find documentation. To get around this, what I did was….try it. Thirty seconds later, and I can confirm pnputil.exe can accept an unc path. Let's take a look at the command.

pnputil.exe /a "\\fileshare\HPPrinter\*.inf"

This command will add the driver to the store at C:\Windows\System32\DriverStore. This example would run against all .inf files in the specified folder. If you know the exact file, it would be easier to add the full name in there.

Install the Driver

Pnputil.exe does not install the driver, but it just makes it so you CAN install the driver. We will do this with the Add-PrinterDriver command.

Add-PrinterDriver -Name "HP OfficeJet 5200 series PCL-3" -InfPath "C:\Windows\System32\DriverStore\FileRepository\hpygid24_v4.inf_amd64_f312bf16a5228084\hpygid24_v4.inf"

One thing I did find out in this was I believed I could put whatever I want in the name section. After many errors, I found out that this is not the case. If you open up the inf file, it will list names that will work. For the example above, I found the name in this section.

So make sure you have this correct before, and the driver should install without issue.

Create Printer Port

Not a lot in this section. Run Add-PrinterPort, name it, and include the IP of the printer you are installing.

Add-PrinterPort -Name "Ports McGee" -PrinterHostAddress "IP Of Printer"

No gotchas, I can't even really add a throwaway line that I think is funny that the editors wish I would stop putting in blogs. They are not my supervisors, and I do what I want.

Install the Printer

Finally, we can run Add-Printer. Point it to the driver you installed, the port you created, and name that thing. In my example, I named it Printy because that is what my daughter called our home printer. I considered coming up with my own name, but honestly, she terrifies me. I can't run the risk that she will see this blog later and punish me for disrespecting her naming scheme.

Add-Printer -DriverName "HP OfficeJet 5200 series PCL-3" -Name "Printy" -PortName "Ports McGee"

Putting it all Together

So what have we learned? Use Group Policy if you can, but if you can't, a little prep work and some PowerShell can still get printers done without a mental breakdown. It will just be four lines.

pnputil.exe /a "\\fileshare\HPPrinter\*.inf"
Add-PrinterDriver -Name "HP OfficeJet 5200 series PCL-3" -InfPath "C:\Windows\System32\DriverStore\FileRepository\hpygid24_v4.inf_amd64_f312bf16a5228084\hpygid24_v4.inf"
Add-PrinterPort -Name "Ports McGee" -PrinterHostAddress "IP Of Printer"
Add-Printer -DriverName "HP OfficeJet 5200 series PCL-3" -Name "Printy" -PortName "Ports McGee"

The next step is running this on all computers that need it. I would recommend PDQ Deploy and PDQ Inventory. You will be able to get each computer you need and installed on those machines in just minutes.

Ready to put the PowerShell scanner through its paces?

Take our 14-day Free Trial. Feel the power of the PowerShell scanner.
Start a Trial

Don't miss the next post!

SigRed Critical Vulnerability and Patch/Fix

SigRed Critical Vulnerability and Patch/Fix

PDQ.com
© 2020 PDQ.com Corporation

Products

  • Deploy
  • Inventory
  • Enterprise SL
  • Pricing
  • Downloads
  • Licensing
  • Buy