Get-Printer

I think Out-Printer is a very handy cmdlet, and one that doesn’t get used much. Pipe any cmdlet to it and the output will be printed to your default printer. You use it the same way you would Out-File except output is printed instead of saved to a file. The cmdlet also has a parameter that lets you specify a  printer. This is very handy if, like me, you have a PDF printer installed. The challenge though is to remember the printer names. To that end I wrote a simple PowerShell script to query the registry and return some basic printer information.

This script works on both PowerShell v1.0 and 2.0 and for admins and non-admins alike.

Function Get-Printer {

 

    $printers=Get-ChildItem  HKLM:System\currentControlSet\Control\print\Printers

    

    foreach ($printer in $printers) {

        $Name=$printer.PSChildName

        $Port=(Get-ItemProperty -Path $printer.PsPath -Name Port).Port

        $PrinterDriver=(Get-ItemProperty -Path $printer.PsPath -Name 'Printer Driver').'Printer Driver'

    

        $obj=New-Object PSObject

        $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $Name

        $obj | Add-Member -MemberType NoteProperty -Name "Port" -Value $Port

        $obj | Add-Member -MemberType NoteProperty -Name "PrinterDriver" -Value $PrinterDriver

       

       write $obj

    

    } #end ForEach

 

} #End Function

The function simply reads the Printer portion of the registry using the HKLM: Registry PSDrve.  The printer name is the subkey of each item under Printers. But I also wanted to get some other information from the associated print driver, which is a sub key for each printer. That’s why I used Get-ItemProperty to get the Port and Printer Driver values for each printer. The function writes a custom object to the pipeline with these values.

PS S:\> get-printer

Name                                    Port                  PrinterDriver

—-                                    —-                  ————-

CutePDF Writer                          CPW2:                 CutePDF Writer

HP psc 2100 Series                      USB001                HP psc 2100 Series

Microsoft XPS Document Writer           XPSPort:              Microsoft XPS D…

Send To OneNote 2010                    nul:                 Send To Microsoft..

SnagIt 9                                C:\ProgramData\Te… Snagit 9 Printer

Or now that I know the printer name I can do this:

PS S:\> get-printer  | out-printer "HP PSC 2100 Series"

Post to Twitter Post to Delicious Post to Facebook Post to StumbleUpon

This entry was posted in PowerShell, Registry, Scripting and tagged , , , . Bookmark the permalink.

10 Responses to Get-Printer

  1. Darrin Henshaw says:

    Nice Jeff, very handy. However, the download link doesn’t seem to work :)

  2. Darrin Henshaw says:

    Perfect, thanks Jeff.

  3. Darrin Henshaw says:

    Hey Jeff,

    Grabbed your function and started testing with it. Works great for local printers, but it didn’t even notice my networked ones. Checked the registry key and there were no entries for my network printers.

    Made a little switch, and used a different registry key but doesn’t have information like driver and port:

    function Get-Printer {

    $printers = @(Get-ChildItem ‘registry::\HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion’|where {$_.name -match “PrinterPorts”}|Select-Object -expand property)

    foreach ($printer in $printers) {
    $Name=$printer.ToString()

    $obj=New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name “Name” -Value $Name

    write $obj

    } #end ForEach

    } #End Function

    Set-Alias gpr Get-Printer
    Get-Printer

    No doubt there are other ways of doing it, but I thought to mention it. Cheers.

    • Jeffery Hicks says:

      I totally forgot about network printers. I’ve been using locally attached printers for so long that it never even crossed my mind. The name is the important part anyway.

      • Jeffery Hicks says:

        If you forget all the stuff about ports, all that matters is the name and I think all you really need is this one liner:

        (get-item “hkcu:\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts”).GetValueNames()

        Use the name as listed for Out-Printer. Networked printers need the UNC. You can still turn this into a function if you’d like:

        Function Get-Printer { (get-item “hkcu:\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts”).GetValueNames() }

        That’s much easier, don’t you think?

  4. Darrin Henshaw says:

    True, I tested it out and all it really needed was the name. Also, never really used Out-Printer before but I can see many uses for it now, particularly the PDF option you mentioned, thanks for the post very useful.

    Cheers,

    Darrin Henshaw
    Twitter: @darrin.henshaw

    • Jeffery Hicks says:

      I totally overthought this and made it much more complicated than necessary. I have no idea where my head was.

      gwmi win32_printer | select Name

  5. Darrin Henshaw says:

    Should have refreshed my screen as I was posting. Definitely the one line is much better. That’s one failing I have is I tend to use more code than neccessary, sometimes makes the script easier to look at and see what’s happening, but sometimes makes it worse.

  6. Darrin Henshaw says:

    Lol, I’m glad I’m not the only one with that failing.