Get default printer remotely

前端 未结 2 932
太阳男子
太阳男子 2021-01-21 05:12

Windows 7:

cscript C:\\Windows\\System32\\Printing_Admin_Scripts\\en-US\\prnmngr.vbs -g

Windows XP:

cscript C:\\windows\\syste         


        
2条回答
  •  时光说笑
    2021-01-21 05:47

    You can use wmi32_printer to get the default. Here is the code:

    $AllPrinters = gwmi win32_printer -computername c78572
    $DefaultPrinter = $AllPrinters | where {$_.Default -eq $true}
    

    This will return all locally attached printers. If you want to get a list of network attached printers (as Aaron commented below), you run into a little bit of an issue. The above script doesn't work because WMI operates on the local machine, and not on the user level. After much research, one way of getting this information is to have a log on script that runs, because there is essentially no other way of remotely using WMI to get the logged in user's information.

    How to really do it if we can't use WMI? Use the back door. All the pertinent information is stored in the registry. The output may not be pretty, but it will give you all the information that we need. We are only concerned about 3 key locations:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers
    

    This contains all Locally Installed printers. Forget about it, use the gwmi win32_printer command to get this list.

    HKEY_CURRENT_USER\Printers\Settings
    

    This contains all the Currently logged in User Installed printers. It does not have the default printer information.

    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device
    

    This is where to get the Currently logged in User Installed Default printer. i.e. This is what Aaron is specifically looking for.

    So, we can use PowerShell to connect to the remote registry, and read the currently logged in user's default printer with the following script:

    $Computer = "c78572"
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('currentuser', $Computer)
    $RegKey= $Reg.OpenSubKey('Software\Microsoft\Windows NT\CurrentVersion\Windows')
    $DefaultPrinter = $RegKey.GetValue("Device")
    $DefaultPrinter | ConvertFrom-Csv -Header Name, Provider, Order| Select Name
    

    ----EDIT - to get a list of all printers----

    To list all printers on the remote computer:

    $Computer = "c78572"
    
    #Get Local Printers:
    $Printers = @(Get-WmiObject win32_printer -computername $Computer | Select Name)
    
    #Get List of Network Printers:
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('currentuser', $Computer)
    $RegKey= $Reg.OpenSubKey('Printers\Settings')
    $Printers += @($RegKey.GetValueNames())
    
    #Output List of Printers
    Write-Output $Printers | ft -Property @{Name="Printer Name";Expression={$_.Name}} -AutoSize
    
    
    #Get Default Printer
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('currentuser', $Computer)
    $RegKey= $Reg.OpenSubKey('Software\Microsoft\Windows NT\CurrentVersion\Windows')
    $DefaultPrinter = $RegKey.GetValue("Device")
    
    #Output the Default Printer
    Write-Output $DefaultPrinter | ConvertFrom-Csv -Header Name, Provider, Order| Select Name | ft -Property @{Name="Default Printer Name";Expression={$_.Name}} -AutoSize
    

提交回复
热议问题