Windows 7 logon scripts — set default printer

printervbscriptwindows 7windows-server-2008-r2

I have been tasked with troubleshooting a logon script at a client site that among other things sets the default printer. The logon script is vbscript with the printer being set using

Set WshNetwork = CreateObject("WScript.Network")

...

WshNetwork.SetDefaultPrinter(strPrinter)

The tech who set it up said it was working. The customer said it wasn't. My testng shows hat it works only intermittently. Currently I've got something like this:

WScript.Echo GetDefaultPrinter
For i = 0 to 50
    if strPrinter <> GetDefaultPrinter then
        WshNetwork.SetDefaultPrinter(strPrinter)
    end if
Next    
'WshNetwork.SetDefaultPrinter("Fax")

Msgbox("Called WshNetwork.SetDefaultPrinter(" & strPrinter & ")")
WScript.Echo GetDefaultPrinter


Function GetDefaultPrinter
sComputer = "."
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = oWMIService.ExecQuery("Select * from Win32_Printer",,48)
For Each oItem in colItems
If (oItem.Attributes And 2^(3-1)) = 4 Then
sDefault = oItem.Name
Exit For
End If
Next
GetDefaultPrinter = sDefault
End Function

This lets me check the default printer before and after attempting to change it. The loop seems to have helped, but has not resolved the problem entirely. Sometimes the new printer is correctly set, sometimes I can log off and on again and it can't set the same default printer. To clarify, I am not a full time admin. Is this technique out of date? Should I be using something else to set the default printer?

Edit: Additionally, we need to remember the default printer that the user may have set or changed during their user session and then set that printer as default

Best Answer

Since you have Server 2008 and Windows 7, you have the ability to set a default printer through Group Policy Preferences. We're using that right now to set default printers in our computer labs. It does require creating a GPO for these containers, but you can get pretty granular with them.

In the Group Policy Editor, the policy is located at...

User Configuration -> Preferences -> Control Panel Settings -> Printers

Add printers in there. One of the check-boxes is "make default printer". It really works.