Powershell List Network Printers on Remote PC

17.3k views Asked by At

I am trying to trouble shoot a GPO to deploy a printer and I need to see what network printers are on a remote machine for the current logged on user. When I do this

Get-WMIObject Win32_Printer -ComputerName PCNAME

I get a list of the local installed printers and when I try this

  Get-WMIObject Win32_Printer -ComputerName PCNAME | where{$_.Name -like “*\\*”} | select sharename,name

I get nothing. Any help? I am using PowerShell 4.0 so the Get-Printer doesn't work.

1

There are 1 answers

0
CraigD On

I borrowed heavily from tukan's code in another thread (thanks... that code bridged a gap I had not yet been able to bridge) and modified it to my purposes.

The code below determines the logged-in user on the specified remote computer, then outputs the printers that user has listed in the Registry under HKU<SID>\Printers\Connections.

As a bonus I added a couple extra lines that output the user's currently-selected default printer.

#- BEGIN FUNCTION -------------------------------------------------------------------------------------------------
Function remote_registry_query($target, $key)
{
   Try {
        $registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("Users", $target)
        ForEach ($sub in $registry.OpenSubKey($key).GetSubKeyNames())
        {
            #This is really the list of printers
            write-output $sub
        }

  } Catch [System.Security.SecurityException] {
        "Registry - access denied $($key)"
  } Catch {
        $_.Exception.Message
  }
}
#- END FUNCTION ---------------------------------------------------------------------------------------------------


##############################
# EXECUTION STARTS HERE
##############################

# Prep variables and get information for the function call


# set the computer name
$computer = "computer_name"


# get the logged-in user of the specified computer
$user = Get-WmiObject –ComputerName $computer –Class Win32_ComputerSystem | Select-Object UserName
$UserName = $user.UserName
write-output " "
write-output " "
write-output "Logged-in user is $UserName"
write-output " "
write-output " "
write-output "Printers are:"
write-output " "

# get that user's AD object
$AdObj = New-Object System.Security.Principal.NTAccount($user.UserName)

# get the SID for the user's AD Object 
$strSID = $AdObj.Translate([System.Security.Principal.SecurityIdentifier])

#remote_registry_query -target $computer -key $root_key
$root_key = "$strSID\\Printers\\Connections"
remote_registry_query -target $computer -key $root_key

# get a handle to the "USERS" hive on the computer
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("Users", $Computer)
$regKey = $reg.OpenSubKey("$strSID\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows")

# read and show the new value from the Registry for verification
$regValue = $regKey.GetValue("Device")
write-output " "
write-output " "
write-output "Default printer is $regValue"
write-output " "
write-output " "
[void](Read-Host 'Press Enter to continue…')