How Can I List All the Printers on a Remote Computer?

How Can I List All the Printers on a Remote Computer?

  • Comments 7
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I list all the printers – both local printers and network printers – on a remote computer?

-- CH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CH. You know, back when he worked at the University of Washington the Scripting Guy who writes this column was asked to speak at a symposium being held as part of a conference on computers and education. To say this was a last-minute invitation would be an understatement; in fact, our hero had to start putting together his talk on the plane ride from Seattle to Florida, then finish it when he reached the convention center. When he arrived at the symposium itself, he discovered that his fellow speakers were hardly as harried as he was. Their secret: instead of creating a new talk for the symposium they decided to just deliver talks that they were already giving at the conference. In other words, they were going to give the exact same talk on two different days, but to two different audiences (or so they hoped).

Note. As a matter of fact it was a very interesting symposium, if only because the future Scripting Guy was the only person on the panel who had a talk even remotely connected to the topic. Needless to say, it proved to be the lowest-rated session at the conference. (And, let’s face it: being voted the most-boring and least-useful session at an academic conference isn’t easy.)

Now, if this future Scripting Guy was smart he would have learned that clever people can do twice the work with half as much effort. Your question, CH, is a good example of that. Because you specifically asked about listing printers on a remote computer, a smart person would write a column about that, then come back a few months later and reprint the column, only this time telling people how to list printers on the local computer. (And a smart person could get away with that because the two scripts are all-but identical.)

Of course, as people who read this column know, the Scripting Guy responsible for all this isn’t particularly smart. Instead of writing two separate columns he could only come up with one, thanks to a script that lists the printers on either a remote computer or a local computer:

strComputer = "atl-ps-01"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
     
For Each objPrinter in colPrinters
    If objPrinter.Attributes And 64 Then 
        strPrinterType = "Local"
    Else
        strPrinterType = "Network"
    End If
    Wscript.Echo objPrinter.Name & " -- " & strPrinterType
Next

As you can see, we begin by assigning the name of the remote computer (in this case, atl-ps-01) to a variable named strComputer. What if we did want to list the printers on the local machine? No problem, and no need to read a separate column, either. Instead, just assign strComputer the name of the local machine, or assign it a dot (WMI shorthand for the local computer):

strComputer = "."

After connecting to the WMI service on the desired computer, we then issue the following query, a query that returns a collection of all the printers installed on the computer:

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

If you weren’t sure just how … smart … the Scripting Guy who writes this column really is, well, what comes next should help convinced you. CH only asked for a list of printers, both local and network; however, no one mentioned whether or not the script could specify which printers were which. That could have been a whole ‘nother column; instead, we simply added a few lines of code that will make that distinction for you.

To make that distinction we first set up a For Each loop that loops through the collection of printers. Inside that loop the very first thing we do is execute this line of code:

If objPrinter.Attributes And 64 Then

What we’re doing here is checking the bitmask property Attributes to see whether bit 64 has been set. If it has, then we know that this is a local printer; in that case we set the value of the variable strPrinterType to Local:

strPrinterType = "Local"

If bit 64 has not been set then this must be a network printer; consequently we assign strPrinterType the value Network:

strPrinterType = "Network"

Two notes about this process. First, we aren’t going to talk about bitmask attributes today; for a reasonably good explanation of these babies take a look at this section of the Microsoft Windows 2000 Scripting Guide..

Second, you might be wondering why we didn’t just check the values of the Win32_Printer properties Local and Network; after all, isn’t the sole purpose of those properties to tell you whether a printer is a local printer or a network printer? Yes it is, and using these two properties is a bit easier and a bit more straightforward than using the Attributes property. However, the Local and Network properties are supported only on Windows XP and Windows Server 2003. By using the Attributes property we were able to create a generic script that will work on any version of Windows that supports WMI.

Which, come to think of it, means we missed out on yet another column: How Can I Determine Printer Types on a Windows 2000 Computer?

Sigh ….

All we have left to do now is echo back the printer name and the printer type:

Wscript.Echo objPrinter.Name & " -- " & strPrinterType

And that should do it.

Incidentally, a professor from the UW happened to attend the symposium where the future Scripting Guy spoke. As she told him the following day, she was shocked, absolutely shocked that the Scripting Guy had poked fun at the university. However, she added, “Seeing as how you’re not a professor and really not all that smart I’m not going to report you to anyone.” You can imagine how gratified this Scripting Guy was to hear that. (Editor’s Note: People at Microsoft say pretty much the same things about this Scripting Guy. At least he’s consistent.)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hey scripting guy,

    I have used the steps discribed in your article to help me develop a diagnosis tool for helpesk

    http://www.ldap389.info/en/2010/03/12/diagnosis-tool-for-your-helpdesk/

    And I have problems when retrieving network printers from a remote host, it works only if I use logged in users credentials :

    http://msdn.microsoft.com/en-us/library/aa389290%28VS.85%29.aspx#specifying_credentials_and_an_authentication_level_for_the_remote_connection

    Is there a trick to bypass this limitation?

    Thanks!

  • Nice article....It is very informative....thanks.....

    <a href="mypcmag.com/.../a>

  • From what I know of the Printer Object in WMI. It only provides the Printers of the person whose credentials are being used to run the query.

    I'm pretty sure there is a way to query the Registry for 'all' printers that are assigned on the computer regardless of who has logged in.

  • Nice script.

    You could have prevented another article by letting people specify the tarcget computer as an argument to the command line!

    I changed line one to

    strComputer = WScript.Arguments.Item(0)

    Now when I run the script I can type

    c:\Scripts\printers-list.vbs" <PCName>

    e.g.

    c:\Scripts\printers-list.vbs" .

    c:\Scripts\printers-list.vbs" DC01

    c:\Scripts\printers-list.vbs" PC123

    Unfortunately, I've tried running this against remote computers and it only gives me the locally installed ones, not ones on a network share :(

  • Hey Scripting Guy!  (Had to say it...)  Would it be possible to see how you would change this script into an IT administrative tool that accomplishes:

    -List all installed printers on ALL computers on the network, under ANY user's login

    We're changing from an old print server, where printers were manually added by IP, or in some cases, added via logon scripts, to a 2008R2 print server, where we intend to push them out via GPO.  So I need to ID every printer each user currently has, then install the same printers (though from the new network location) BEFORE we retire the old print server.

    Thanks!

    karmana1....h0tm@!l

  • how to write the code to get the list of printer object that is connected with lan

  • Hi,

    I am bit new to automation i just want to know how to query the Printer remotely in bulk and rename it if discrepenacy is found