Enumerate printers for a specific Terminal Server session

VVS picture VVS · Mar 24, 2009 · Viewed 9.3k times · Source

I am looking for a way to see what printers a specific user has mapped into his or her TS session.

How can I achieve this with WMI (via PowerShell) or VB-Script? Is there a built-in way I'm not aware of?

EDIT: In our construct mapping of local printers by the RDP-Client is disabled. Users get their printers created during login via VBS-Script and deleted during logoff.

So there's no printers installed directly on our TS server and querying the Win32_Printers WMI class returns nothing. The printers are installed on a dedicated print server. Querying the printers on that server returns ALL printers and not the one mapped for a single user.

Answer

VVS picture VVS · Jul 20, 2010

Thanks to Remko's comment I was put into the right direction and finally made a script that did what I needed.

Basically the script determines the SID of the user and looks in the user's registry hive (HKEY_USERS\$sid\Printers\Connections) for the created printers.

Here's the quick and dirty powershell script:

$server = 'servername'
$userName = 'username'

$regHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $server)
$regProfileList = $regHKLM.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")

foreach ($sid in $regProfileList.GetSubKeyNames())
{
    $profileImagePath = $regProfileList.OpenSubKey($sid).GetValue("ProfileImagePath")
    if ($profileImagePath.EndsWith("\$userName"))
    {
        $regHKU = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("Users", $server)
        $regUser = $regHKU.OpenSubKey("$sid\Printers\Connections")
        foreach ($printer in $regUser.GetSubKeyNames())
        {
            $printer.Replace(",", "\")  # backslashes are replaced with commas, revert that
        }
    }
}