Go Back   Computer Forums > General Computing > Software and Operating Systems
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 06-01-2012, 06:22 AM   #1
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Batch file to remove printer by name

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?
__________________

BikerEcho is offline   Reply With Quote
Old 06-02-2012, 02:25 AM   #2
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Re: Batch file to remove printer by name

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
__________________

__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Old 06-02-2012, 07:15 AM   #3
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Batch file to remove printer by name

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.
BikerEcho is offline   Reply With Quote
Old 06-02-2012, 08:17 AM   #4
Baseband Member
 
Join Date: Nov 2011
Posts: 86
Default Re: Batch file to remove printer by name

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!
Rackmountsales is offline   Reply With Quote
Old 06-02-2012, 08:45 AM   #5
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Batch file to remove printer by name

Quote:
Originally Posted by Rackmountsales View Post
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.
BikerEcho is offline   Reply With Quote
Old 06-03-2012, 06:56 PM   #6
Baseband Member
 
Join Date: Apr 2012
Location: USA
Posts: 44
Default Re: Batch file to remove printer by name

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.
chirkware is offline   Reply With Quote
Old 06-03-2012, 07:06 PM   #7
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Batch file to remove printer by name

nice. thanks man. i will check it out this wednesday.
BikerEcho is offline   Reply With Quote
Old 06-06-2012, 07:30 AM   #8
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Batch file to remove printer by name

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)
__________________

BikerEcho is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 01:30 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0