Announcement

Collapse
No announcement yet.

Script to remove obsolete printer in local profiles

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to remove obsolete printer in local profiles

    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.

  • #2
    Re: Script to remove obsolete printer in local profiles

    Dave, search the forum please.
    There are quite a few examples about it in here.
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"

    Comment


    • #3
      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=pr...Amicrosoft.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 & "\root\cimv2")
      
      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%\system32\prndrvr.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

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        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

        Comment


        • #5
          Re: Script to remove obsolete printer in local profiles

          Originally posted by Daveinholland View Post
          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?
          ** Remember to give credit where credit is due and leave reputation points where appropriate **

          Comment


          • #6
            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\\printserver\sharename
            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

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              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?
              Regards,
              Jeremy

              Network Consultant/Engineer
              Baltimore - Washington area and beyond
              www.gma-cpa.com

              Comment


              • #8
                Re: Script to remove obsolete printer in local profiles

                Originally posted by Wired
                did you perhaps copy / paste this from another forum?
                It was: http://forums.petri.com/showthread.php?t=12421

                Originally posted by Jeremy
                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
                Last edited by Rems; 8th January 2007, 19:53.

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: Script to remove obsolete printer in local profiles

                  Originally posted by Rems View Post
                  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.
                  Regards,
                  Jeremy

                  Network Consultant/Engineer
                  Baltimore - Washington area and beyond
                  www.gma-cpa.com

                  Comment


                  • #10
                    Re: Script to remove obsolete printer in local profiles

                    Originally posted by Daveinholland View Post
                    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_USER\Printers\Settings]
                    [HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices]
                    [HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts]
                    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers]
                    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro l\Print\Printers]
                    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardwa re Profiles\Current\System\CurrentControlSet\Control\ Print\Printers]
                    [HKEY_USERS\.DEFAULT\Software\Microsoft\Windows NT\CurrentVersion\Devices]
                    [HKEY_USERS\.DEFAULT\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts]
                    [HKEY_CURRENT_CONFIG\System\CurrentControlSet\Contr ol\Print\Printers\Canon iP90 - Photo]

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

                    "DefaultSpoolDirectory"="C:\\WINDOWS\\System32\\sp ool\\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.
                    Cheers,

                    Rick

                    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                    2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

                    Comment


                    • #11
                      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 & "\root\cimv2")
                      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="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\DefaultSpoolDirectory"
                      
                      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
                      Last edited by Rems; 8th January 2007, 22:42.

                      This posting is provided "AS IS" with no warranties, and confers no rights.

                      __________________

                      ** Remember to give credit where credit's due **
                      and leave Reputation Points for meaningful posts

                      Comment


                      • #12
                        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.

                        Comment


                        • #13
                          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.

                          Comment


                          • #14
                            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_USER\Printers\Connections\
                            (?)

                            \Rem

                            This posting is provided "AS IS" with no warranties, and confers no rights.

                            __________________

                            ** Remember to give credit where credit's due **
                            and leave Reputation Points for meaningful posts

                            Comment


                            • #15
                              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_USER\Printers\Connections\
                              (?)

                              Thanx a lot! THat did it! At least on one client. Do you know a vbs script that can delete this j\key? HKEY_CURRENT_USER\Printers\Connections

                              Comment

                              Working...
                              X