Batch file to remove printer by name

BikerEcho

Fully Optimized
Messages
4,029
Location
Denmark
Hi guys.
now i usually fix my problems myself or ask google, but this is just getting annoying.
i have some laptops at work which has a network printer installed.
but this printer does no longer exist. we had it removed because that it did not work properly anymore.
so i gave the new printer a name and share name that is more suitable for the position of the printer.
now the employees and students have to choose between 2 printers. one that works and one the don't.
the new printer is added every time via a batch script from the server.
the reason for that is that the laptops have reborn cards in them and it will only apply for each user.
and we have 400 users and new users came from time to time.

so my question is: how can i via the same batch scripts running on login also remove the not working printer only via the printers name.
i can't use "//thyregod/printers/indskoling" because that distination don't exist.
i can't seem to find the share name on the printer ether because when i right-click on the printer it says "could not find the printer"
do you guys have any idea?
 
We used a GPO to do this but another tech created it. So I searched for a GPO method and found this. Hopefully it works.

Code:
On Error Resume Next

' This script will be run against the local computer
strComputer = "."

' The next two set commands will query all the printers on the local computer
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
    
' Iterate through the printers found
For Each objPrinter in colInstalledPrinters
    ' Uncomment for some debugging (the printer's name)
    'Wscript.Echo "Name: " & objPrinter.Name
    
    If objPrinter.Name == "Name_of_old_printer" Then
        objPrinter.Delete_
    End If
Next
    
' Once old printer is gone add the new one

Set WshNetwork = CreateObject("WScript.Network")

' Uncomment the second line if you want to set the new printer as the default

WshNetwork.AddWindowsPrinterConnection "\\PrintServer1\Name_of_new_printer"
'WshNetwork.SetDefaultPrinter "\\PrintServer1\Name_of_new_printer"

Wscript.Quit
 
thank you for your post. i will check it out when i go to work again this Wednesday.
i'll let you know how it went.
 
RUNDLL32 printui.dll,PrintUIEntry /n \\server\printer.name /dn
The ending /dl is a local printer delete the ending /dn is a network printer delete,
rundll32 printui.dll,PrintUIEntry /y /q /n \\server\printer.name
Hope that helps!
 
RUNDLL32 printui.dll,PrintUIEntry /n \\server\printer.name /dn
The ending /dl is a local printer delete the ending /dn is a network printer delete,
rundll32 printui.dll,PrintUIEntry /y /q /n \\server\printer.name
Hope that helps!

thanks for the comment, but that wont work.
i don't have the destination for the printer because it does no longer exist.
i don't even know what the destination was when the printer was still on the domain.
so the \\server\printername wont work.
 
To find the actual \\server\printer name, go to the registry of one of the affected machines, and look at:
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Print\Providers\LanMan Print Services\Servers

Each server will have its own folder. Inside that folder, you'll find a string value called "Share Name".

Now that you have that, RackMountSales method should work. For that matter, you could do a .vbs script with the WshNetwork.RemovePrinterConnection command to delete the printer.
 
thanks for your help guys. i did however find another easier way to do it.
reg delete "hkcu\printers\connections" /f
deletes every printer added in regestry.

so my file looks like:
_______________________
@echo off

echo tjekker/fixer printere setup
echo.
echo step 1/3
reg delete "hkcu\printers\connections" /f

cls
echo tjekker/fixer printere setup
echo.
echo step 2/3
start \\THYREGOD\indskoling
if errorlevel 1 goto AddPrinterError

cls
echo tjekker/fixer printere setup
echo.
echo step 3/3
rundll32 printui.dll,PrintUIEntry /y /q /n \\THYREGOD\indskoling
exit

:AddPrinterError
msg * Kunne ikke tilslutte printeren. tjek dit internet ellers Kontakt IT-team hvis problemer bliver ved
ping localhost -n 4 >nul
__________________________

works fine^^

there is allot of echos, but it is necessary so the user knows what is going on in case it takes a little longer then it should (which it does sometimes)
 
Back
Top Bottom