Hello. I’ve been searching all over and have found some resources that should have yielded me the results I was looking for, but haven’t.
I am trying to determine the default printer that is installed for a remote user. In the following script, I am pointing the script to a remote PC (where the user is logged in), grabbing the SID for the user and then pointing the script at the registry key to get the default printer from that SID. It fails to do so.
$username = “user123”
$domain = “mydomainhere”
$Computer = “testcomputer”
$SID = [Get-WmiObject -Class Win32_UserAccount -Filter “Domain = ‘$domain’ AND Name = ‘$username’”].SID
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey[‘currentuser’, $Computer]
$RegKey = $Reg.OpenSubKey[‘$SID\Software\Microsoft\Windows NT\CurrentVersion\Windows’]
$DefaultPrinter = $RegKey.GetValue[“Device”]
Write-Host $DefaultPrinter
Getting the error:
[b]You cannot call a method on a null-valued expression.
At C:\Users\cverhage\Desktop\Untitled6.ps1:8 char:35
- $DefaultPrinter = $RegKey.GetValue <<<< ["Device"]
- CategoryInfo : InvalidOperation: [GetValue:String] , RuntimeException
- FullyQualifiedErrorId : InvokeMethodOnNull[/b]
I"ve also tried something like this:
[b]$AllPrinters = gwmi win32_printer -computername testcomputer
$DefaultPrinter = $AllPrinters | where {$_.Default -eq $true}
Write-Host $DefaultPrinter[/b]
This works when I run it from my PC, against my PC but does not give me a result when I run it against another PC.
Any suggestions on how to do this?
Resources that I"ve used:
http://stackoverflow.com/questions/20639541/get-default-printer-remotely
http://powershell.com/cs/forums/p/14466/28166.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2004/11/17/hey-scripting-guy-is-there-any-way-to-determine-the-default-printer-on-a-computer.aspx
https://social.technet.microsoft.com/Forums/windowsserver/en-US/14f33784-09a0-49be-8036-73921181fa3c/microsoftwin32registrykeyopenremotebasekey?forum=winserverpowershell