Powershell Printer Script

Some detail is shown to the user, this form isn’t modal so won’t take over the screen, and if they close it the script carries on in the background.

Over the last 15 years I’ve tried pretty much every method of adding printers at logon there is – KIXTART script, VBS, Group Policy Preferences and Powershell. As part of speeding up logon, and investigating a weird issue with Windows 10 printers, I moved away from GPP and to Powershell shortly after we upgraded from Windows 8.1 to Windows 10.

The issue being – roughly 5% of the time, on random user/computer combinations, printers would take a long time adding and then fail to add, with a non-specific error message. My first go at this was a basic powershell script which had a hard coded list of location/printer mapping, and it would run the “add printer” command repeatedly until the error went away. (It always added fine on the 2nd go). The problem with this is that it’s a complicated script for technicians to update, and being a single threaded script the nice form it displays showing people what’s happening would freeze while it was working in the background.

My new script does the bulk of the work in background jobs – so printers add quicker (as it can do more than one at once), and the UI doesn’t lock up and freeze. More importantly, it uses Group Policy Preferences by reading the XML file generated and applies that – so technicians have the familiar interface for adding/removing printers from the script. 

Add your printer connections and re-order, if you have a “Delete All” it MUST come first.

First step for this is to create a group policy object – call it anything you like, mine is “Printers v3”. Make sure that you DO NOT link it to any OU (or if you do, the link is disabled) as we do not want this processed by the group policy client, this is purely to generate the XML file for the powershell script.

I’ll leave a detailed howto on group policy preferences but you should be able to find what you need with a quick search on the Internet.

The script uses the User Configuration set of preferences – as you can’t add shared printers to the Computer section, and currently only supports shared (i.e. networked and shared off a Windows server) printers.

Set up your list of printers – it doesn’t matter whether you select Create, Replace or Update as they all do the same thing in this instance. If you set multiple printers to be default, whichever runs last will win.

You can use item level targeting to determine which groups/computers/users/OUs get which printers – it supports collections, which basically apply like brackets around the logic, so you can be pretty targeted if required.

Now on to the script. You will need the GUID of the policy, this can be found in Group Policy Management Console by selecting the GPO, then going onto the “Details” tab, copy the Unique ID and insert that where prompted near the start of the script, along with your fully qualified domain name (on the same screen, this is “Domain” at the top).

As the preferences file is XML it’s easy to read into powershell.

When displaying the printer details to the end user, it will show whatever is down as the printer name in the GPP section. By default this is the share name, however you can rename this by selecting the item and hitting F2.

Set this as the logon script in Group Policy – you will need to make sure you are allowing all scripts to run (Computer Configuration->Policies->Windows Components->Windows Powershell->Turn on Script Execution) or set it as a non-Powershell logon script with the command:

powershell.exe -executionpolicy bypass -file \\path\to\printers.ps1"

The Printers logon script can be found in my GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.