I wrote a PowerShell script to install all printer drivers on a Citrix or Terminal Server.

Actually the script isn’t specific to Citrix or Terminal Server but on such environments we need to preload all drivers because users do not have the permissions to do that.

I have chosen for PowerShell because you can do it in a one-liner which makes it easy to run this script from my Altiris server on all Citrix Servers.

The idea is that we enumerate all the shared printers on a Printer Server and make a connection to each printer. This will make sure that the driver is installed if it wasn’t already present.

The script could even be scheduled to enforce that newly added printer drivers are added to each Citrix Server.

To enumerate all printers we can use the Win32_Printer WMI class like this:

It’s possible that some printers are not shared so we are going to filter that out using the -filter parameter:

Our next step is to make a printer connection and MSDN shows that the win32_printer class has an AddPrinterConnection method.

So I first tried:

I am not sure of the exact reason but obviously there is no access to the AddPrinterConnection method.

This works though:

Now we can use the For-Each Object to make a connection to each printer using the __SERVER property for the servername and the ShareName property for the Printername:

Once again I met a PowerShell oddity: I couldn’t use AddPrinterConnection(“\\$_.__SERVER\$_.ShareName”) so I used:

And finally we put the whole thing in a one liner, replacing double quote (“) with (‘) and using the gwmi alias to make the line shorter:

EDIT: In order to load new driver we need to enable the SeLoadDriverPrivilege. I have corrected the code above,  see Enabling Privileges for WMI in PowerShell for an explanation.