I got a question from Mike this week:
“Hi Steve. I have a question. We have three remote branches and users move from branch to branch with their laptops. Is there any way to change their default printer when they login at each branch?”
Further discussions with Mike gave me more info: Most of the latops are running Windows XP, each branch has their own DHCP server, and each branch uses a different gateway.
If he was using Windows 7 on all those laptops, we would not need a script – this feature is built in.
My method will use VBScript. Lets start out by creating a script that will get the current default gateway:
Function GetDefaultGateway
Dim Adapter, Adapters, WMI
GetDefaultGateway = ""
Set WMI = GetObject("winmgmts:\\.\root\cimv2")
Set Adapters = WMI.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each Adapter In Adapters
If Not IsNull(Adapter.DefaultIPGateway) Then
If Not Adapter.defaultIPGateway(0) = "0.0.0.0" Then
GetDefaultGateway = Adapter.DefaultIPGateway(0)
Exit For
End If
End If
Next
End Function
Our function above “GetDefaultGateway” will loop through our network interfaces and find the one with its gateway set…when it finds that it will exit and return it as a string.
Now, we need to check the gateway, and set the default printer based on what we see:
Dim oPrinter
Set oPrinter = CreateObject("WScript.Network")
Select Case GetDefaultGateway
Case "192.168.0.1"
oPrinter.SetDefaultPrinter "\\ServerName\Branch1Printer"
Case "192.168.1.1"
oPrinter.SetDefaultPrinter "\\ServerName\Branch2Printer"
Case "192.168.2.1"
oPrinter.SetDefaultPrinter "\\ServerName\Branch3Printer"
Case else
oPrinter.SetDefaultPrinter "Xerox Phaser 6130N PS"
end case
So there we have it. If the user’s gateway matches one of our cases, we set the appropriate default printer…if nothing matches we set it to the Xerox printer.
Notice that you can provide full printer names, or a path to its network share – either will work.
The full script can be found here
Just rename to .vbs, and add your own settings and you should be good to go.
One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools
{ 8 comments… read them below or add one }
Hi!
I wrote this batch code that does the same in less lines.
@echo off
ipconfig|find “192.168.0.1”>nul
if %errorlevel% == 0 (rundll32 printui.dll,PrintUIEntry /y /q /n “\\ServerName\Branch1Printer”&&exit)
ipconfig|find “192.168.1.1”>nul
if %errorlevel% == 0 (rundll32 printui.dll,PrintUIEntry /y /q /n “\\ServerName\Branch2Printer”&&exit)
ipconfig|find “192.168.2.1”>nul
if %errorlevel% == 0 (rundll32 printui.dll,PrintUIEntry /y /q /n “\\ServerName\Branch3Printer”&&exit)
rundll32 printui.dll,PrintUIEntry /y /q /n “Xerox Phaser 6130N PS”
Greetings 🙂
Awesome – Thanks for sharing
Cool Scripts guys. I have been using a different way to do this, and it is based on machine name. This works great when you have desktop systems that cannot be moved. Never thought of using the gateway. This will work perfect in my situation.
Hi Steve,
I was wondering if you knew if this script would work on Windows 2000
I am not sure if it will. The only tricky part would be if the Windows 2000 version of VBScript has the proper WMI items to support my GetGateway function. Just give it a go and see what happens. Let us know what you find 🙂
Good Script. Thanks for sharing
Does anyone know if there is a way to set the tray of the printer in VB Script?
Where is this feature in Windows 7?