Script to remove obsolete printer in local profiles

Home Forums Scripting Windows Script Host Script to remove obsolete printer in local profiles

This topic contains 23 replies, has 7 voices, and was last updated by Avatar Daveinholland 12 years, 6 months ago.

Viewing 24 posts - 1 through 24 (of 24 total)
  • Author
    Posts
  • #119681

    Hi

    We use local profiles. There are a lot of obsolete printers still retained in the profiles. I woulkd like to start with a clean slate. Delete all these printers and then use a script to add them again. So basically the question is how do i remove all printers when a user logs on.

    Avatar
    Dumber
    Participant
    #196163

    Re: Script to remove obsolete printer in local profiles

    Dave, search the forum please.
    There are quite a few examples about it in here.

    Rems
    Rems
    Moderator
    #226152

    Re: Script to remove obsolete printer in local profiles

    I used a computer-logoff-script once after a server migration. It removes the printers *and* it’s drivers when the computer shutsdown.

    For deleteting the drivers i used the microsoft script prndrvr.vbs that comes with windows
    http://www.google.com/microsoft?q=prndrvr.vbs+site%3Amicrosoft.com

    Code:
    ‘- – – – – – – – – – – – – – – – – – –
    ‘First delete all Printers from the computer
    ‘(in my case i excluded two printers, used for pdf creation on
    ‘ several workstations)
    ‘Then delete the TCPIP-printer-ports that are not used anymore by a printer
    ‘Finaly, un-install the drivers that are not currently used by a printer

    on Error Resume Next
    Set objDictionary = CreateObject(“Scripting.Dictionary”)

    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

    Set colPrinters = objWMIService.ExecQuery _
    (“Select * from Win32_Printer”)
    ‘***delete all Printers from the computer
    For Each objPrinter in colPrinters
    If Not objPrinter.Name=”Jaws PDF Creator” Then
    If Not objPrinter.Name=”Acrobat Distiller” Then
    ObjPrinter.Delete_
    End If
    End If
    Next

    ‘***delete the TCPIP-printer-ports
    For Each objPrinter in colPrinters
    objDictionary.Add objPrinter.PortName, objPrinter.PortName
    Next

    Set colPorts = objWMIService.ExecQuery _
    (“Select * from Win32_TCPIPPrinterPort”)
    For Each objPort in colPorts
    If objDictionary.Exists(objPort.Name) Then
    Else
    ObjPort.Delete_
    End If
    Next

    ‘***un-install the printer drivers
    Set WSHShell = CreateObject(“WScript.Shell”)
    WSHShell.Run “Cscript.exe %windir%system32prndrvr.vbs -x -s %computername%”, 0, False

    ‘- – – – – – – – – – – – – – – – – – –

    The script is old, it was once part of a longer script, so better test before using it.

    RemS

    #290954

    Re: Script to remove obsolete printer in local profiles

    Thanx Rems this workjs great however maybe you or someone knows the following:

    When I want to delete the old printernames on the windows xp client systems it simply tells me that the printer doesn’t exist on the server and therefore cannot delete it. Also using a script the script will only delete the actual printers that have still valid names on the server. For instance:

    I changed a printername from sales to helpdesk. In the local printer settings from a user i cannot delete the old sales printer. Not even manually. However when I change the name back on the printserver from helpdesk to sales i can delete it again.

    However this would be a bit of an arduous task changing all the printernames back again and then using the script to delete the names. Is there way to force the deletion of these printers?

    I have 100 clients and the obsolete printers have to go. Is it possible to delete these in the registry? This way I could use group policy to delete the keys when a user loggs in. I already saw some keys but deleting these doesn’t actually delete them from control panel-> printers. PLs help

    Daveinholland
    View Public Profile
    Send a private message to Daveinholland
    Find all posts by Daveinholland

    #2 8th January 2007, 13:07

    Avatar
    Wired
    Moderator
    #271807

    Re: Script to remove obsolete printer in local profiles

    Daveinholland;52030 wrote:
    Thanx Rems this workjs great however maybe you or someone knows the following:

    When I want to delete the old printernames on the windows xp client systems it simply tells me that the printer doesn’t exist on the server and therefore cannot delete it. Also using a script the script will only delete the actual printers that have still valid names on the server. For instance:

    I changed a printername from sales to helpdesk. In the local printer settings from a user i cannot delete the old sales printer. Not even manually. However when I change the name back on the printserver from helpdesk to sales i can delete it again.

    However this would be a bit of an arduous task changing all the printernames back again and then using the script to delete the names. Is there way to force the deletion of these printers?

    I have 100 clients and the obsolete printers have to go. Is it possible to delete these in the registry? This way I could use group policy to delete the keys when a user loggs in. I already saw some keys but deleting these doesn’t actually delete them from control panel-> printers. PLs help

    Daveinholland
    View Public Profile
    Send a private message to Daveinholland
    Find all posts by Daveinholland

    #2 8th January 2007, 13:07

    did you perhaps copy / paste this from another forum?

    Rems
    Rems
    Moderator
    #226155

    Re: Script to remove obsolete printer in local profiles

    Dit you only manualy changes the printernames on the server?
    And the sharenames are still the same?

    – What is the status of these printers on the client?
    – And what is the exact error you receive on the client when you manualy try to remove the printer? (in your language).
    – On what way are the printer installed on the clients? (manual, somescript, kix)

    You could try this bath first before you run the vbs-script,

    DelSpecificPrinter.cmd

    Code:
    ::IF the printershare exists in current userprofile!, THEN
    ::this batch will try to remove this printer for allusers.
    start/w sc STOP spooler &&start/w sc START spooler
    rundll32 printui.dll,PrintUIEntry /dn /q /n\printserversharename

    Use the Sharename of the printer, and for each printer a new line
    After this, still run the VBS-script at shutdown.

    I’am afraid before you can run the batch at logon/off it is possible you have to give users special permissions first. You can temporarily make them local Administrators, but maybe this policy wil work too:

    Windows Settings / Security Settings / Local Policies
    – User Rights Assignment / “Load and unload Driver”
    -> Add: users
    – Security Options / “Devices: Prevent users from installing printer drivers”
    -> Disabled

    Can you test on one of the clients if this works?

    Rem

    JeremyW
    JeremyW
    Moderator
    #268536

    Re: Script to remove obsolete printer in local profiles

    Just a quick note: Windows won’t delete a printer until the queue is cleared. Is it cleared?

    Rems
    Rems
    Moderator
    #226156

    Re: Script to remove obsolete printer in local profiles

    Wired wrote:
    did you perhaps copy / paste this from another forum?

    It was: http://forums.petri.com/showthread.php?t=12421

    Jeremy wrote:
    Just a quick note: Windows won’t delete a printer until the queue is cleared. Is it cleared?

    Hmmm, yes I’am almost sure the spooler isn’t empty.

    Dave,
    Check the spooler on the server first:
    Wait till you are sure no one is using any printers!!!
    Now First check the location of the spooler folder;
    – Open “Printers and Faxes”
    – click in the menu on “File” and select “Server properties”
    – go to tab “Advanced”
    – Copy the path of the spooler
    – Paste that path in Windows Explorer
    Now Stop the spooler server on the printserver [Start | Run: SC STOP spooler ]
    – Delete all *.SPL and *.SHD files in that directory (make sure no users are currently waiting for a print, because that will be deleted too. OR.. delete the very old file only)
    Now Start the spooler server again [Start | Run: SC START spooler ]
    wait a few seconds before the clients are allowed to print again.

    (You can also check the spooler on the client, the same way)

    Now try to delete the printers…

    Rem

    JeremyW
    JeremyW
    Moderator
    #268539

    Re: Script to remove obsolete printer in local profiles

    Rems;52072 wrote:
    Check the spooler on the server first:

    Note also that if a printer is Offline you can still load up the queue. This means that each client computer needs to be checked.

    Avatar
    rvalstar
    Member
    #286575

    Re: Script to remove obsolete printer in local profiles

    Daveinholland;52030 wrote:
    I have 100 clients and the obsolete printers have to go. Is it possible to delete these in the registry?

    I took a look in the registry and found at least the following 9 +/- spots you’ll need to touch:

    [HKEY_CURRENT_USERPrintersSettings]
    [HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionDevices]
    [HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionPrinterPorts]
    [HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionPrintPrinters]
    [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlPrintPrinters]
    [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetHardware ProfilesCurrentSystemCurrentControlSetControlPrintPrinters]
    [HKEY_USERS.DEFAULTSoftwareMicrosoftWindows NTCurrentVersionDevices]
    [HKEY_USERS.DEFAULTSoftwareMicrosoftWindows NTCurrentVersionPrinterPorts]
    [HKEY_CURRENT_CONFIGSystemCurrentControlSetControlPrintPrintersCanon iP90 – Photo]

    not to mention JeremyW’s point on the print spooler being clear at:

    “DefaultSpoolDirectory”=”C:\WINDOWS\System32\spool\PRINTERS”

    So here’s my thinking:

    Add back dummy printers on your server to match the obsolete names and use the script to delete them.

    That way the registry and spooler dir mess is handled properly.

    You can drop the dummy printers in a week or two.

    Rems
    Rems
    Moderator
    #226157

    Re: Script to remove obsolete printer in local profiles

    To clear the local spool folder, add this code at the top of the computer shutdown-vbs-script;

    Code:
    strComputer = “.”
    intSleep = 3000
    strService = ” ‘Spooler’ ” ‘ NB strService is case sensitive!.

    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” _
    & strComputer & “rootcimv2”)
    Set colListOfServices = objWMIService.ExecQuery _
    (“Select * from Win32_Service Where Name =”_
    & strService & ” “)
    For Each objService in colListOfServices
    objService.StopService()
    WSCript.Sleep intSleep
    Next

    Set WSHShell = WScript.CreateObject(“WScript.Shell”)
    strRegKey=”HKLMSOFTWAREMicrosoftWindows NTCurrentVersionPrintPrintersDefaultSpoolDirectory”

    DeleteFile WSHShell.RegRead(strRegKey) & “*.SPL”
    DeleteFile WSHShell.RegRead(strRegKey) & “*.SHD”

    Set colListOfServices = objWMIService.ExecQuery _
    (“Select * from Win32_Service Where Name =”_
    & strService & ” “)
    For Each objService in colListOfServices
    objService.StartService()
    WSCript.Sleep intSleep
    Next

    ‘Wscript.quit(0)

    function DeleteFile(WhatFile)
    on Error Resume Next
    set fso=createobject(“Scripting.Filesystemobject”)
    fso.DeleteFile(WhatFile)
    End Function

    (To Stop | Start a service on the workstation, you must be a member of the local administrators, or run it as a computerscript)

    Rem

    #290955

    Re: Script to remove obsolete printer in local profiles

    Thankx rems gonna try all this and let u know however to answer ryour previous question: Dit you only manualy changes the printernames on the server?
    And the sharenames are still the same?

    Yes I did, this is exactly what I did.

    #290956

    Re: Script to remove obsolete printer in local profiles

    You also asked the status of the printers on the clients:

    In english it is:

    Cant’s find printer on the server. Cannot make a connection

    Original message dutch:

    Kan printer niet op server vinden. kan geen verbinding maken.

    When I manually try remove I get:

    Original dutch message:

    Kan verbinding met printer niet verwijderen: U hebt ofwel een printernaam opgegeven die niet juist is of de opgegeven printer is niet langer verbonden met de server.

    English translation:

    Can’t remove connection with printer: You have either given a printername which is notcorrect or the given printer is not longer connected with server.

    They were installed with a script however the script does not have these printers in them any more.

    And like I said before I only manually changed the printernames on teh server the shares remain the same so teh name on the serve rwas printer1 shared as printer1 and i just changed the name to sales and left it shared as printer 1.

    I can’t remove it on the client., however I can add this printer again under the name sales again, however the old printer remains there, which makes it look liek a mess.

    Rems
    Rems
    Moderator
    #226158

    Re: Script to remove obsolete printer in local profiles

    English translation:“You either entered a printer name that was incorrect or the specified printer is no longer connected to the server. Click Help for more information”

    Search the registry by the “printername” shown in Windows XP;
    use this tool: Registry Search Tool (it takes about 10 to 50 seconds before it comes with results!)
    Save the results! of each of the printers that gave errors when deleting (maybe that information can help to script the deletejob for the other computers)

    Now, can you try to delete one of these printers manualy-> by deleting the corresponding registrykey of that printer in; HKEY_CURRENT_USERPrintersConnections
    (?)

    Rem

    #290957

    Re: Script to remove obsolete printer in local profiles

    Now, can you try to delete one of these printers manualy-> by deleting the corresponding registrykey of that printer in; HKEY_CURRENT_USERPrintersConnections
    (?)

    Thanx a lot! THat did it! At least on one client. Do you know a vbs script that can delete this jkey? HKEY_CURRENT_USERPrintersConnections

    #290958

    Re: Script to remove obsolete printer in local profiles

    Dim WshShell, bKey

    Set WshShell = WScript.CreateObject(“WScript.Shell”)

    WshShell.RegDelete “HKCUPrintersConnections”

    ok I found this script and it works however of course the users don’t have rights to delete this key I have to find a good way of doing this

    #290959

    Re: Script to remove obsolete printer in local profiles

    Well strange thing is I can delete the registry key manually however I cannot delete the registry key with the previous script oit says unable to remove registry key. I thought it was a rights issue however under full admin rights I get the same error. Any ideas?

    Avatar
    Dumber
    Participant
    #196176

    Re: Script to remove obsolete printer in local profiles

    Dave,

    Maybe start posting the error messages.
    But what’s you’re final goal????
    Migrating printers to a new server?

    Rems
    Rems
    Moderator
    #226160

    Re: Script to remove obsolete printer in local profiles

    I still wondering why it is not possible to delete the printers the normal way :confused:

    Well this is a script what will do, that you are trying to do (delete *all* printers connected in the users profile);

    Code:
    On Error Resume Next

    Const HKEY_CURRENT_USER = &H80000001

    strComputer = “.”
    strKeyPath = “Printers”

    Set objRegistry = GetObject(“winmgmts:\” & _
    strComputer & “rootdefault:StdRegProv”)

    DeleteSubkeys HKEY_CURRENT_USER, strKeypath

    Sub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath)
    objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
    For Each strSubkey In arrSubkeys
    DeleteSubkeys HKEY_CURRENT_USER, strKeyPath & “” & strSubkey
    Next
    End If

    objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
    End Sub

    But that still leave other registrations of the printer(s) in different registry keys though.

    Microsoft wrote:
    Network Printing Registry Entries
    It is recommended that you do not directly edit the registry unless there is no other alternative. Modifications to the registry are not validated by the registry editor or by Windows before they are applied, and as a result, incorrect values can be stored. This can result in unrecoverable errors in the system. When possible, use Group Policy or other Windows tools, such as Microsoft Management Console (MMC), to accomplish tasks rather than editing the registry directly. If you must edit the registry, use extreme caution.

    So better wait to run the script, because someone might come up with a better plan how to cleanup the stale networkprinters in the local user profiles.

    Rem

    Avatar
    rvalstar
    Member
    #286576

    Re: Script to remove obsolete printer in local profiles

    Dave:

    To confirm:

    You can not delete the old printer from a workstation because it does not exist on the server?

    If so, as I stated earlier, add the old printers back on the server (dummy ones if need be), run the deletes on the workstation, and then clean up the server in a week or so.

    I must be missing something???

    #290960

    Re: Script to remove obsolete printer in local profiles

    Rick,

    Dummy ones won’t work, i simply get the message that this printer allready exists. Rems I will try and do the job with ur script I am hoping that there won’t be any adverse effects if i just delete the printers from the HKCUconnectionsprinters key.

    Avatar
    Mouse
    Member
    #290815

    Re: Script to remove obsolete printer in local profiles

    Good morning, hope this script helps. It will remove all network based printers from the profile so just use it as a startup script:

    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootcimv2”)

    Set colInstalledPrinters = objWMIService.ExecQuery _
    (“Select * from Win32_Printer Where Network = TRUE”)

    For Each objPrinter in colInstalledPrinters
    objPrinter.Delete_
    Next

    With regards to this line “(“Select * from Win32_Printer Where Network = TRUE”)”:
    If you want to remove all network printers just leave as TRUE
    Remove all local printers set to FALSE
    Remove all printers regardless of location remove “Where Network = TRUE”

    #290961

    Re: Script to remove obsolete printer in local profiles

    Rems the script worked like a charm, and finally deleted that key, however when I do a search with the registry key finder is till get all these key entries.

    Could your script be adapted to delete these? I will test it on a system first to see if that has any adverse affects

    [HKEY_CURRENT_USERPrintersSettings]
    [HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionDevices]
    [HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionPrinterPorts]
    [HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionPrintPrinters]
    [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetContro lPrintPrinters]
    [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetHardwa re ProfilesCurrentSystemCurrentControlSetControl PrintPrinters]
    [HKEY_USERS.DEFAULTSoftwareMicrosoftWindows NTCurrentVersionDevices]
    [HKEY_USERS.DEFAULTSoftwareMicrosoftWindows NTCurrentVersionPrinterPorts]
    [HKEY_CURRENT_CONFIGSystemCurrentControlSetContr olPrintPrinters

    Sorry mouse your script did tremove the printers however also left the other entries in the registry. Once again I am not sure if it matters leaving them in does any one know?

    #290962

    Re: Script to remove obsolete printer in local profiles

    Thank you for your help, the issue has been resolved. Through the script I was able to remove the connections in the registry by using a logoff script.

    Then I modified the logon script for the correct printers

    Thanx specially to Rems who provided the correct script

Viewing 24 posts - 1 through 24 (of 24 total)

You must be logged in to reply to this topic.