December 12th, 2006

How Can I Add a Network Printer at Logon and, Optionally, Make That the Default Printer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add a network printer at logon? I’d also like to make that the default printer unless the user has a local printer. In that case I don’t want to make the network printer the default printer.

— DB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DB. So, how are … things ….? That’s really some kind of weather we’ve/you’ve been having lately, isn’t it?

Sorry; this is proving to be a very difficult column for us to write. As we noted yesterday, the Scripting Guy who writes this column is on vacation; in fact, the column you’re reading right now was actually written several days ago. You’d think that this wouldn’t make much difference when it comes to a daily scripting column; after all, there isn’t a lot of breaking news in the scripting world. But we Scripting Guys don’t want to be like other daily scripting columns; to help differentiate ourselves from the competition we like to sprinkle our columns with references to current events and to the outside world. But it’s hard to reference current events when the column is being written in advanced, and those events haven’t actually happened yet.

Note. Well, sure, of course Peter can do it. But Peter, as the oldest living Scripting Guy, also has an unfair advantage: he’s so much older than the rest of us that he’s already seen everything that could ever happen, usually more than once. That’s not true for youngsters like the other Scripting Guys.

What that all means, of course, is that we have absolutely nothing to write about today. Sorry.

Pardon? Write about scripting? Well, that seems a bit out-of-place for a daily scripting column. But, then again, we are a little desperate today. So what the heck:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = TRUE")
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\PrintServer1\Xerox300"
If colPrinters.Count = 0 Then
    objNetwork.SetDefaultPrinter "\\PrintServer1\Xerox300"
End If

As you can see, we start out by connecting to the WMI service on the local computer. Can we also use this script against remote computers? Actually no; that’s because we’re going to use Windows Script Host (WSH) to map the network printer, and WSH can perform that task on only the local machine. But that’s OK, because, as a logon script, we don’t need to use this against remote machines. Logon scripts always run locally.

After connecting to the WMI service we use the following query to return a collection of all the local printers on the computer:

Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = TRUE")

Keep in mind that the preceding query (and thus the preceding script) works only on Windows XP, Windows Server 2003, and Windows Vista computers. Why? Because those are the only operating systems in which the Win32_Printer class includes the Local property. But don’t fret; we’ll show you a Windows 2000 version of this script in just a moment.

After we issue our WQL query we create an instance of the Wscript.Network object, then use the AddWindowsPrinterConnection method to add the network printer \\PrintServer1\Xerox300:

Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\PrintServer1\Xerox300"

This gives us a connection to our network printer. Now the question is this: should we make this printer the default printer? To answer that we need to know whether or not the user has any local printers installed. How do we know that? Remember the query we ran, the one that returned a collection of all the local printers? All we have to do is check the value of the collection’s Count property. If the Count is equal to 0 that means no local printers are installed. In that case, we can then use this line of code to set this new printer as the default printer:

objNetwork.SetDefaultPrinter "\\PrintServer1\Xerox300"

If the Count is not equal to 0 that means we have at least one local printer installed. And so we do nothing at all.

Speaking of nothing at all, we won’t bother to explain how to perform this same task on a Windows 2000 computer. However, we’ll at least give you a script that does so:

blnLocal = FALSE
strComputer = "."
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
        blnLocal = TRUE
    End If
Next
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\PrintServer1\Xerox300"
If blnLocal = True Then
    objNetwork.SetDefaultPrinter "\\PrintServer1\Xerox300"
End If

Incidentally, as we were writing this we realized, “Hey, we don’t have to talk about current events. If something happened in the past we can bring it up and no one will ever notice.” With that in mind, how about that French Revolution, huh? Was that crazy or what?

Author

0 comments

Discussion are closed.