powershellregistry

How do I get list of printers from HKU SIDs in the registry?


I cannot seem able to open the HKEY_Users base key in a remote registry. I am trying to use the PowerShell script below to poll the registry to find the list of printers that a user has installed under their profile. Given that we have a proxy , and I cannot change the GPOs for the domain, much easier methods are out of reach for me.

$sid = Get-ADUser -Identity userid | Select-Object -ExpandProperty SID
$comp = "computerName"
$strKeyPrinterList = [string]::Concat($sid,"\Printers\Connections\")
$strRegType = [Microsoft.Win32.RegistryHive]::Users
$strRegKey  = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($strRegType, $comp)
$strRegKey.OpenSubKey($strKeyPrinterList) | Out-GridView

The result is an empty table, with columns for Name and Property, but nothing under them. What am I doing wrong? How do a get a list of the folders under the HKU\SID\Printers\Connections\ key?

I expected a list of subkeys in the format ,,serverName,printerName.


Solution

  • How do a get a list of the folders [subkeys] under the HKU\SID\Printers\Connections\ key?

    Call the .GetSubKeyNames() method on the [Microsoft.Win32.RegistryKey] instance returned by .OpenSubkey():

    $strRegKey.OpenSubKey($strKeyPrinterList).GetSubKeyNames() | Out-GridView
    

    Note that this will only show the names of the subkeys, not also their content.


    As for what you tried:

    The result is an empty table, with columns for Name and Property, but nothing under them.

    The reason is that PowerShell's for-display output-formatting system, as seemingly also used by Out-GridView, relies on [Microsoft.Win32.RegistryKey] instances to be decorated with ETS (Extended Type System) properties.

    Only when such instances are retrieved via the PowerShell registry provider are they decorated with these properties, resulting in rich for-display formatting with both Get-Item (for the targeted key itself) and Get-ChildItem (for the targeted key's subkeys), as well as
    Out-GridView.

    The following minimal example illustrates this difference:

    # OK: Shows the target key's values' names and data,
    #     mediated by ETS properties.
    # Same as:
    #   Get-Item HKCU:\Console | Out-GridView
    Get-Item registry::HKEY_CURRENT_USER\Console | Out-GridView
    
    # !! Despite in effect targeting the same key, this does NOT work the same, 
    # !! due to lack of ETS properties.
    [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Console') | Out-GridView