Introduction
In Remote Desktop Protocol (RDP) sessions, there is a common issue that users may face with invalid redirected printers. These printers may show up in certain applications, specifically some legacy systems. The core of this problem lies in the way the Print Spooler handles the registry entries associated with these printers. Over time, this can lead to the accumulation of invalid registry entries, causing confusion and operational issues. This article explores the nature of the problem and provides guidance on how to address it.
Overview
Remote Desktop Protocol (RDP) sessions are used to remotely access and control computers. However, some applications in an RDP session may present users with invalid printers. This occurs due to the way the Print Spooler adds a registry entry for each redirected printer. These entries are found under the following registry subkey for the user and for all users logged onto the RD Session Host server:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices
The registry entries for these redirected printers are supposed to be deleted when users disconnect or log off. However, in cases where other users may have logged off in the meantime, the registry entries can be leaked into their HKEY_CURRENT_USER registry hive.
Over time, the registry subkey will gather many invalid registry entries for redirected printers. These entries can include permutations of the client name, the printer name, and the session ID. Applications that read this registry subkey will then display these invalid printers to users. It’s essential to note that this issue is more likely to affect some legacy applications.
VB Script to Clear Redirected Printers
- Create a VB script called delprinter.vbs with the following content:
In this particular code, the script is accessing the Windows Registry to enumerate and delete specific values related to redirected printers and printer ports. It appears to be targeted at removing registry entries for devices and printer ports that contain the substring “redirect” within an RDP session.
Const HKEY_CURRENT_USER = &H80000001 on error resume next strComputer = "." Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Devices" oReg.EnumValues HKEY_CURRENT_USER, strKeyPath, arrDevices For Each device In arrDevices if (inStr(device,"redirect")) then oReg.DeleteValue HKEY_CURRENT_USER,strKeyPath,device WScript.echo "Deleted DEVICE - " & device else WScript.eCho "No Action DEV - " & device end if Next strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" oReg.EnumValues HKEY_CURRENT_USER, strKeyPath, arrPrinterPorts For Each printerport In arrPrinterPorts if (inStr(printerport,"redirect")) then oReg.DeleteValue HKEY_CURRENT_USER,strKeyPath,printerport WScript.echo "Deleted PRINTERPORT - " & printerport else WScript.eCho "No Action PPORT - " & printerport end if Next
- Create Group Policy that runs when users log off.
- Reference the batch file you created in the previous step.