Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get-Printer

Posted on October 16, 2009October 16, 2009

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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"


Behind the PowerShell Pipeline
Download Get-Printer.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

10 thoughts on “Get-Printer”

  1. Darrin Henshaw says:
    October 16, 2009 at 11:55 am

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

    1. Jeffery Hicks says:
      October 16, 2009 at 12:07 pm

      I fixed the download link. Please try again.

  2. Darrin Henshaw says:
    October 16, 2009 at 12:34 pm

    Perfect, thanks Jeff.

  3. Darrin Henshaw says:
    October 16, 2009 at 2:19 pm

    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.

    1. Jeffery Hicks says:
      October 16, 2009 at 2:33 pm

      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.

      1. Jeffery Hicks says:
        October 16, 2009 at 3:04 pm

        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:
    October 16, 2009 at 3:07 pm

    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

    1. Jeffery Hicks says:
      October 16, 2009 at 3:19 pm

      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:
    October 16, 2009 at 3:10 pm

    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:
    October 16, 2009 at 3:21 pm

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

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d