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:
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)
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:
Set oPrinter = CreateObject("WScript.Network")
Select Case GetDefaultGateway
oPrinter.SetDefaultPrinter "Xerox Phaser 6130N PS"
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