ScriptBuilder can automatically add network printers based on the computer name, username or group membership. If you add printers based on the computer name, you might find that users accumulate a large collection of printers as they move from PC to PC. This problem will occur for users with roaming user profiles, as the printer connections are stored in the users profile. To remove any existing network printers, follow the procedure below:
- Open ScriptBuilder
- Click the “Code editor” icon
- Expand the Global (All Scripts) section.
- Select “Footer”
- Paste the following code into the text box to the right:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
sub RemoveNetworkPrinters Dim i Dim objPrinters,objNetwork Dim PrinterPath set objNetwork = WScript.CreateObject("WScript.Network") Set objPrinters = objNetwork.EnumPrinterConnections for i = 0 to objPrinters.Count - 1 PrinterPath = objPrinters.Item(i) ' * Only remove network printers if mid(PrinterPath, 1, 2) = "\\" then objNetwork.RemovePrinterConnection PrinterPath, True, True end if next end sub |
Example Below:
- Click the “Header” section under Global (All Scripts)
- Click “Yes” to save changes.
- Enter the following text into the text box on the right: RemoveNetworkPrinters
- Click “Exit” and click “Yes” to save changes.
The next time the script is built and run, the code added in the procedure above will ensure that any existing network printer connections are removed before the script is run. If you open the script file produced by script builder you will notice that the “RemoveNetworkPrinters” function has been added to the end of the script. You will also notice that this function is called at the beginning of the script.
Note: If a particular user wants to maintain a connection to a shared printer, you will need to add the printer to the script under Users or Groups section. If a user connects to a printer manually, that printer connection will be removed the next time the script is run. Also note that the “Code editor” section of ScriptBuilder has so far been undocumented. An incorrect modification can stop the script from working entirely, so please test the script after making any modifications. Upgrades to ScriptBuilder in the future have the potential to break any custom functionality added using code editor.