Occasionally Windows logon scripts need the RemovePrinterConnection method. So I will provide two worked
examples of how to remove unwanted printer icons from the Printers and Faxes folder.
I find that printer logon scripts do not need RemovePrinterConnection as much as mapping network drives
need RemoveNetworkDrive. The reason is that if you map the same printer repeatedly you don't get the error: 'The local device name is already in use'. Surprisingly, the script just maps the new printer
over the old one.
Nevertheless, removing an existing printer is useful when your scripts are at the testing stage and you don't want to keep going to
the Printer and Faxes folder, right-clicking the printer and selecting: Delete.
To get rid of a printer icon, most of the time, all that you need is the method, RemovePrinterConnection and the printer name.
For example,
objNetwork.RemovePrinterConnection
strUNCPrinter (Network Printer) or objNetwork.RemovePrinterConnection "Epson" (Local Printer)
Mastering the basics of RemovePrinterConnection are easy. If there is a danger it's adding, un-needed, unnecessary syntax. For instance there is no need for a comma before the printer name.
Even mastering the
extra arguments is not difficult, however, in this instance you do need commas between the last three arguments, here is the full syntax:
It was a long time before I realized that the prefix b, in bForce and bUpdateProfile meant Boolean. Once I understood this significance,
it became obvious why there were only two possible values, true or false. bForce means remove
the printer - even if its in use. (My friend 'Mad' Mick thinks bForce means brute force and he is not far wrong.)
Setting bUpdateProfile to "true" tells VBScript to update the user's profile.
Even though RemovePrinterConnection is straightforward, we still need preparation. So we must create a printer, otherwise, there would be nothing to remove and all you would get is Error: 'This
network connection does not exist'
You need an printer before the script has any icons to remove from the Printers and Faxes folder.
' PrintersLong.vbs - Windows Logon Script example. ' VBScript - to map a network printer ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.2 - April 24th 2010 '
----------------------------------------------------' Option Explicit Dim objNetwork Dim strUNCPrinter strUNCPrinter = "\\alan\Epson" Set objNetwork =
CreateObject("WScript.Network") objNetwork.AddWindowsPrinterConnection strUNCPrinter WScript.Echo "Check the Printers folder for : " & strUNCPrinter
Thus utility makes it easy to check the health of a router or firewall.
Check the real-time performance, and availability statistics, for any
device on your network. Get started with an extensive collection
of "out-of-the-box" monitors for popular network devices. Give Network
Monitor a whirl - it's free.
Download your free Network Device Monitor
Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
Save the file with .vbs extension e.g. RemovePrinterConnection.vbs.
Double click the VBScript and then launch your Printers and Faxes folder. There should be no printer corresponding to strUNCPrinter.
' RemovePrinterConnection.vbs - Windows logon script ' VBScript to - Network Printer ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.4 - April 24th 2010 '
------------------------------------------------------' Option Explicit Dim objNetwork, strUNCPrinter strUNCPrinter = "\\LittleServer\win2pdf"
Set objNetwork = CreateObject("WScript.Network") '
Section which removes the network printer objNetwork.RemovePrinterConnection strUNCPrinter
Just in case you need the bForce or the bUpdateProfile switch, here is an example of the full RemovePrinterConnection command with all three arguments.
Instructions to RemovePrinterConnection
You need to create a printer before this script can delete its icon from the Printers and Faxes folder.
Copy and paste the script below into notepad.
Save the file with .vbs extension e.g. RemPrinterBForce.vbs.
Double click the VBScript and then launch your Printers and Faxes folder. There should be no printer corresponding to strUNCPrinter name.
'
' RemPrinterbForce.vbs - Windows logon script sample ' VBScript to - Network Printer ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.5 -
April 24th 2010 ' ------------------------------------------------------' Option Explicit Dim objNetwork, strUNCPrinter, bForce, bUpdateProfile strUNCPrinter = "\\LittleServer\win2pdf" bForce = "True"
bUpdateProfile = "False"
Set objNetwork = CreateObject("WScript.Network") ' Section which removes the network printer objNetwork.RemovePrinterConnection strUNCPrinter, _ bForce, bUpdateProfile
Note 1: Spot the bForce, and bUpdateProfile commands, "true" and "false" respectively. You may wish to replace with "true", "true" in your production script.
Note 2: In the
RemoveNetworkDrive method the bForce makes a difference, but here with RemovePrinterConnection it does not make much difference. My point is that even without bForce, the script deleted my printer, even
though it had a print job. I even explicitly added bForce "False". Anyway, no big deal.
Guy
Recommends: Permissions Analyzer - Free Active Directory Tool
I like the
Permissions Monitor because it enables me to see quickly WHO has permissions
to do WHAT. When you launch this tool it analyzes a users effective NTFS
permissions for a specific file or folder, takes into account network share
access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free utility saves when you are
troubleshooting authorization problems for users access to a resource.
Give this permissions monitor a try - it's free!
RemovePrinterConnection is a logon script method for deleting printers. One use is clearing up old printers, but my main use is in testing when I want to save a journey to the
Printers and Faxes folder.
The extra features you get in your eBook
include, more pages full of detailed examples. Also, ten 'how to...'
sections, with screen shots showing which menus to use. Go for Guy's
eBook - and get a printable version with copy enabled and no expiry date.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.