Change default printer based on location

I would like to write VBScript to change the default printer based on which the printer is connected.
I have a laptop that I use at work and at home, and I would like to run this script when Windows starts, so the default printer is always correct.
If in XP there is another way to do this, I’m all ears.

+3
source share
1 answer

WMI may be required.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'ScriptedPrinter'")
For Each objPrinter in colInstalledPrinters
If objPrinter.Name="SomePrinterName" Then 
    objPrinter.SetDefaultPrinter()
End If
Next

- http://msdn.microsoft.com/en-us/library/aa394598(VS.85).aspx

You can also find out the domain, etc., for example:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings 
    Wscript.Echo "System Name: " & objComputer.Name
    Wscript.Echo "Domain: " & objComputer.Domain
Next

- http://msdn.microsoft.com/en-us/library/aa394586.aspx

+2

All Articles