Set Terminal Server user default printer at login

1.4k Views Asked by At

I manage a terminal server in our production environment that serves many different machines that connect under the same generic ID. Using the Cassia library, I am easily able to capture everything I need to know about the remote connections programmatically, but I am as of yet stumped as to how I need to go about applying the printer change to that user. My distinguishing criteria will be the name of the PC that s/he is connecting from. There are 4 machines in particular that I am interested in, and the rest will be ignored.

In a nutshell, I need to capture when these particular PCs log on, and set a default printer for them for the remainder of their session.

At a local level, it is no challenge to change a default printer programmatically. I seem to be struggling with changing the default printer for a user's session on a remote terminal server from another utility server.

2

There are 2 best solutions below

1
On

The easiest way to do this is create a script that is set to run on login and set the printer via a WMI to Win32_Printer.SetDefaultPrinter.

Here is a example of how to do it in a simple 3 line powershell script

$Printers = Get-WmiObject -Class Win32_Printer
$Printer = $Printers | Where{$_.Name -eq 'Name Of Printer To Use'}
$Printer.SetDefaultPrinter()

Save that in a .ps1 file and set the terminal server policies to run the script on login.

If you don't want to use a fixed name in the script it is easily modifiable to query some other external source, just replace 'Name Of Printer To Use' with a variable that holds the name you want to connect to.

The SetDefaultPrinter method is supported on Windows Vista/Server 2003 and up.

0
On

This sets the default printer for the current user on a Terminal server.

$strPrinter = "\\ITPG-FP01\Kyocera A4 ZW"
$printer = Get-WmiObject -Class Win32_Printer | Where-Object { $_.Name -eq $strPrinter }

if ($printer) {
    $deviceId = $printer.DeviceID
    $network = New-Object -ComObject WScript.Network
    $network.SetDefaultPrinter($deviceId)
}