Get printer names, IP addresses and drivers from a computer using WMI.

kickapoos 1 Reputation point
2021-04-06T18:44:34.06+00:00

Hello,
I found the following PowerShell script in this forum, it works very well. Is it possible to export the users and groups of the installed printers.
If possible, not the SID but the username in plain text

$ReportFileName = "c:\printerreport.csv" 
$PrintServersList="c:\PrintServersList.txt" 

$servers =  Get-Content -Path $PrintServersList 
$allprinters = @() 
foreach( $server in $servers ){ 
Write-Host "checking $server ..." 
$printers = $null 
$printers = Get-WmiObject -class Win32_Printer -computername $server | select Name,Shared,ShareName,Local, DriverName, PortName,@{n="PrinterIp";e={(((gwmi win32_tcpipprinterport -ComputerName $server -filter "name='$($_.PortName)'") | select HostAddress).HostAddress)}},@{n='PrintServer';e={$_.SystemName}}, Location,Comment,SpoolEnabled,Published
$allprinters += $printers 
 } 
 Write-Host "exporting to printers.csv" 
$allprinters | Export-CSV -Path $ReportFileName -NoTypeInformation -Force -Encoding UTF8
Write-Host "Done!"
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,353 questions
{count} votes

7 answers

Sort by: Most helpful
  1. kickapoos 1 Reputation point
    2021-04-07T05:14:05.59+00:00

    Hi,

    The users and groups of the printers found are to be exported to c: \ printerreport.csv.

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,476 Reputation points Microsoft Vendor
    2021-04-07T07:53:07.63+00:00

    Hi,

    Do you want to get the names in the security tab? If so you can try something like this

    $ReportFileName = "C:\printerreport.csv"   
    $PrintServersList="C:\PrintServersList.txt"   
          
    $servers =  Get-Content -Path $PrintServersList   
    $allprinters = @()    
    foreach( $server in $servers ){   
    Write-Host "checking $server ..."   
    $printers = $null   
    $printers = Get-WmiObject -class Win32_Printer -computername $server |   
        select Name,Shared,ShareName,Local, DriverName, PortName,  
        @{n="PrinterIp";e={(((gwmi win32_tcpipprinterport -ComputerName $server -filter "name='$($_.PortName)'") | select HostAddress).HostAddress)}},  
        @{n='PrintServer';e={$_.SystemName}}, Location,Comment,SpoolEnabled,Published,  
        @{n='Trustee Name';e={($_.GetSecurityDescriptor()).Descriptor.DACL.Trustee.Name -join ','}},  
        @{n='Trustee SID';e={($_.GetSecurityDescriptor()).Descriptor.DACL.Trustee.SIDString -join ','}}  
        $allprinters += $printers    
      }  
        
    Write-Host "exporting to printers.csv"   
    $allprinters | Export-CSV -Path $ReportFileName -NoTypeInformation -Force -Encoding UTF8  
    Write-Host "Done!"  
    

    Note that some of the trustees have no name.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. kickapoos 1 Reputation point
    2021-04-07T11:47:21.657+00:00

    Hi IanXue-MSFT,

    Great !!!

    Thank you, all entries are in the CSV.
    At the end, two errors are displayed

    checking  ...
    Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null 
    or empty, and then try the command again.
    At C:\printerlistexport_acl.ps1:9 char:63
    + ... printers = Get-WmiObject -class Win32_Printer -computername $server |
    +                                                                 ~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
    
    exporting to printers.csv
    Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
    At C:\printerlistexport_acl.ps1:19 char:17
    + ... lprinters | Export-CSV -Path $ReportFileName -NoTypeInformation -Forc ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
    
    Done!
    
    0 comments No comments

  4. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,476 Reputation points Microsoft Vendor
    2021-04-08T01:26:43.537+00:00

    Hi,

    The error message indicates the last element of $servers is empty. Please check if there's a blank line at the end of PrintServersList.txt.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  5. kickapoos 1 Reputation point
    2021-04-08T18:36:37.523+00:00

    Hi,

    I removed the space in PrintServersList.txt, it works.

    The admin,,ALL APPLICATION PACKAGES,,CREATOR_Owner,Administrators are duplicated in printerreport.csv.

    The authorization list from the printer shows

    ALL APPLICATION PACKAGES = doc_manage and print
    S-1-15-3-1024 = doc_manage and print
    CREATOR_Owner = doc_manage
    ADMIN = full
    Administrators = full
    hx-101= print

    In the printerreport.csv

    Trustee Name
    hx-101,admin,admin,CREATOR_Owner,ALL APPLICATION PACKAGES,ALL APPLICATION PACKAGES,ALL APPLICATION PACKAGES,Administrators,Administrators,,,
    

    I also tested it on different 2019 servers

    0 comments No comments