views:

759

answers:

2

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.

+2  A: 

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

Remou
A: 

Hi,

You can do this, I made a little app in vb.net that does that very thing. I used WMI, if you look at my website I have the app and source posted up there. You can check it out and do with it as you please.

here is the link:

Change Default Printer Based On Network IP or SSID

Anthony Selby