VBS script that will delete old orphaned network printer mappings from client machines dynamically without knowing share name?

VBS script that will delete old orphaned network printer mappings from client machines dynamically without knowing share name?

I would like to be able to edit my current printer VBS script and add the ability to delete any orphaned/old network printer mappings that clients have.  We have taken over this client and alot of the users have around 8-10 old printer mappings that connect to nowhere (share names don't exist anymore).  Is there a way to script it to just delete the orphaned network share connections?

I attached a portion of our printer script... the current script has the ability to delete printers if the network printer share name is know (bottom of script) - but I'd like to know if its possible to do it without knowing the share name and just by checking if the mapping even has a valid connection.
Set WshNetwork = CreateObject("WScript.Network")
On Error Resume Next
IF PrinterPath = "\\CLIENT\IR4080PS" then
PrinterPath = "\\CLIENT\IR4080PS"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
end if
 
On Error Resume Next
IF PrinterPath = "\\CLIENT\ColorPS" then
PrinterPath = "\\CLIENT\ColorPS"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
end if
 
 
 
On Error Resume Next
IF PrinterPath = "\\CLIENT\Canon5020-PS" then
PrinterPath = "\\CLIENT\Canon5020-PS"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
end if

Open in new window

Locutus_NYCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Locutus_NYCAuthor Commented:
RTFM huh.. sorry and thanks :)
0
sr75Commented:
This function will delete all mapped printers.  I suggest you it that BEFORE mapping printers.
Function func_DelMapPrinters()
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	'
	'  This function will parse through a collection of 
	'  printers and delete each one.
	'
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
	DIM strComputer
	DIM strImpLvl
	DIM strQuery
	DIM WMI
	DIM colPrinters
	DIM Printer
 
	strComputer = "."
	strImpLvl = "winmgmts:" & "{impersonationLevel=impersonate}!\\"
	strQuery = "Select * from Win32_Printer"
 
	set WMI = GetObject(strImpLvl & strComputer & "\root\cimv2")
	set colPrinters = WMI.ExecQuery
 
	For Each Printer in ColPrinters
		Net.RemovePrinterConnection Printer.Name
	Next
 
	set strComputer = Nothing
	set strImpLvl = Nothing
	set strQuery = Nothing
	set WMI = Nothing
	set colPrinters = Nothing
	set Printer = Nothing
 
End Function

Open in new window

0
and235100Commented:
Thank you. Happy to point a useful resource out...
0
canaliCommented:
The easy solution is delete all network printer mappings and recreate only the right printers
' delete all networked printer
Dim oShell
Set oShell = WScript.CreateObject ("WScript.shell")
cmd="cmd.exe /c reg delete HKCU\Printers\Connections /f"
oShell.run cmd
Set oShell = Nothing
'


 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows XP

From novice to tech pro — start learning today.