VBS script used to remove network printer
I would like to write a VBS script that helps to remove a network printer and should not display any error message if a network printer is not available for a particular user. Please help me.
I would like to write a VBS script that helps to remove a network printer and should not display any error message if a network printer is not available for a particular user. Please help me.
Hi Jessica,
You can try to use the below sample code. It will remove all printers then add the printers specified at the bottom. Please do take note that this code only works in XP and not yet tested on Vista.
Â
'==========================================================================
'
' NAME: RemoveAllPrinters.vbs
' COMMENT: Removes network printers then add printers specified at the bottom (XP tested Only)
'
'==========================================================================
ON ERROR RESUME NEXT
Set wshNet = CreateObject("WScript.Network")
Set wshPrn = wshNet.EnumPrinterConnections
For x = 0 To wshPrn.Count – 1 Step 2
If Left(wshPrn.Item(x+1),2) = "\" Then wshNet.RemovePrinterConnection wshPrn.Item(x+1),True,True
Next
wshnet.AddWindowsPrinterConnection "\serverprinter1"
wshnet.AddWindowsPrinterConnection "\serverprinter2"
wshnet.SetDefaultPrinter "\serverprinter2"
'============================================================
Aristono
This VB script will only work for Windows XP.
i) VB Code
Remove all Network printers but not local printers
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set Printers = WshNetwork.EnumPrinterConnections
For i = 0 to Printers.Count – 1 Step 2
If Left(ucase(Printers.Item(i+1)),2) = "\" Then
       WScript.Echo Printers.Item(i+1)
       WSHNetwork.RemovePrinterConnection Printers.Item(i+1)
   End IF
Next
ii) You have to check the printer server name.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
Set colInstalledPrinters =Â objWMIService.ExecQuery _
   ("Select * from Win32_Printer Where Network = TRUE")
For Each objPrinter in colInstalledPrinters
   objPrinter.Delete_
Next
For Windows 2000, you have to manually remove each network printers.
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.RemovePrinterConnection \PrintServerxerox3006
To stop displaying error messages, here is the VB code:
 i)  Sub Main()
     Dim args() As String
     Dim argument As String
     args = Environment.GetCommandLineArgs()
     Try
        ProcessFile(args(1))
     Catch
        Console.WriteLine("ERROR")
     End Try
     Console.WriteLine("Press enter to end")
     Console.ReadLine()
  End Sub
  Sub ProcessFile(ByVal fName As String)
     'process file code goes here
     Console.WriteLine("Am processing " & fName)
  End Sub
ii) Sub Main()
  Dim args(), argument As String
  args = Environment.GetCommandLineArgs()
  Try
     ProcessFile(args(1))
  Catch indexProblem As IndexOutOfRangeException
     Console.WriteLine("ERROR – No file name _
        supplied")
  Catch ioProblem As System.IO.IOException
     Console.WriteLine("ERROR – can't process file _
        named " & args(1))
  End Try
  Console.WriteLine("Press enter to end")
  Console.ReadLine()
End Sub