Question

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

Was it helpful?

Solution

WMI may suit.

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 and such like:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top