Installing Printers with PowerShell
Automate it with Add-User
August 22, 2013
In "Managing Printers Gets Easier in Windows 8," I observed that many folks have decided to skip Windows 8 thanks to its (ahem) less-than-sterling reputation—but those who have decided to adopt Windows 8 are discovering a bevy of hidden treasures. Among those gems are 20 useful printer-related cmdlets. In that article, I showed how the add-printerdriver cmdlet lets you designate one or more printer drivers as essentially "safe for the non-admin user to use," paving the way for a cmdlet that I'll cover this time: add-printer.
As you've probably already guessed, add-printer lets you install a printer. That doesn't sound exactly scintillating—except for two things. First, add-printer is a command-line tool, which makes automating it easy. Second (and here's the really nice part), non-administrative users can run it, which means that automating it is as simple as putting the cmdlet in a user's logon script. (If that sounds troubling, remember that a user can't create a printer unless you approve that printer's driver.)
Add-Printer Options
The add-printer cmdlet has a lot of options, but here's the basic syntax:
add-printer -name -drivername -port
The first option is just the name you want to appear in Devices and Printers (e.g., "Upstairs laser"). The driver name is the same name you used in add-printerdriver (e.g., Brother HL-4040CDN Series, HP Deskjet 5700 Series, Dell Color Laser 1320c). But what about the port name? You've seen the PowerShell nouns printer and printerdriver, but there's one more to learn: printerport.
You can see your system's printer ports by typing
get-printerport
You'll probably get a lot of results. I created the port named ToColor. I had a network-attached printer on my intranet—a Dell 1320c sitting on 10.50.50.50—that I wanted to connect to a workstation.
So, I had this much of the add-printer cmdlet so far:
add-printer -name "ColorDell" -drivername "Dell Color Laser 1320c"
What about -port? If the printer were directly USB-attached, I could use -port USB001, but I wouldn't because the system's built-in Plug and Play (PnP) infrastructure identifies the port name automatically. But if I wanted to connect to a network-attached printer, then my system needs a little help. Assuming I know the IP address or DNS name of the printer, PowerShell's add-printerport lets me create a port to that address. So, if my Dell 1320C is at 10.50.50.50, I can create a network port to it with the command
add-printerport -name "ToColor" -printerhostaddress "10.50.50.50"
That's very simple, because I really only have to give the port a name ("ToColor") and an IP or DNS address. (You need only standard user privileges to create printerports with PowerShell.) With the printerport in place, any user with access to the Dell 1320C drivers can connect, like so:
add-printer -name "ColorDell" -drivername "Dell Color Laser 1320c" -port "ToColor"
What About Shared Printers?
So you can connect to a local printer with a simple add-printer cmdlet in two situations: A locally attached printer needs just a name and a driver's name, and a network-attached printer needs one more thing: a port. But those two scenarios leave out the most common situation, which is connecting to a printer shared via a server.
To connect to one of those, you need only an option: -connectionname . For example, suppose I've connected that Dell 1320C printer ("ColorDell") to a server called Netdoor and shared that printer as etdoorPL. My workstation could then connect with just -connectionname, as in
add-printer -connectionname etdoorPL
In that case, I'd end up with a printer named etdoorColorDell and yes, you read that right: PowerShell gives that connection a name that isn't blindly equal to the UNC path but rather blends the server's name and printer name perceived by the print server.
Printer Removal
What about disconnecting? If you're even a bit PowerShell-adept, you'll already know that PowerShell's verb for deleting or eliminating something is remove, and you'll have guessed that you delete a printer with remove-printer, as in
remove-printer "Downstairs Printer"
Always remember that although I often use uppercase and lowercase in my examples to make them a bit more readable, PowerShell is almost always case-insensitive. So, remove-printer "DoWNstaiRS PRINTER" would work just as well.
About the Author
You May Also Like