{"id":31,"date":"2008-07-29T00:56:19","date_gmt":"2008-07-28T15:56:19","guid":{"rendered":"http:\/\/www.jhouseconsulting.com\/index.php\/jhouseconsulting\/2008\/07\/29\/script-to-remove-orphanedstale-printer-objects\/"},"modified":"2013-01-07T10:49:46","modified_gmt":"2013-01-07T02:49:46","slug":"script-to-remove-orphanedstale-printer-objects","status":"publish","type":"post","link":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/2008\/07\/29\/script-to-remove-orphanedstale-printer-objects-31","title":{"rendered":"Script to remove orphaned\/stale printer objects"},"content":{"rendered":"<p>I wrote this script to help a customer clean up old orphaned and stale printer objects from user profiles. This script can safely be executed in each user session and works a treat.<\/p>\n<p>However, you may first want to try my <a href=\"http:\/\/www.jhouseconsulting.com\/index.php\/jhouseconsulting\/2008\/07\/30\/script-to-remove-printers\/\" target=\"_blank\">script to remove printers<\/a> the traditional way.<\/p>\n<p>Here is the <a href=\"http:\/\/www.jhouseconsulting.com\/2008\/07\/29\/script-to-remove-orphanedstale-printer-objects-31\/removeorphanedprinters-vbs\" rel=\"attachment wp-att-907\">RemoveOrphanedPrinters.vbs<\/a>\u00a0script.<!--more--><\/p>\n<pre class=\"brush: vb; auto-links: false; title: ; toolbar: false; notranslate\" title=\"\">\r\n\r\nOption Explicit\r\n\r\n' This script will remove registry keys and values. It was specifically written to clean out\r\n' old orphaned\/stale printer objects from a users registry hive that cannot be removed using\r\n' the conventional methods.\r\n' If any registry key or value contains the strings listed in the arrOrphanedPrinters array,\r\n' it will be removed.\r\n\r\n' This script could easily be modified to rename printer objects.\r\n\r\n' Written by Jeremy@jhouseconsulting.com on 28th July 2008.\r\n\r\n' Note that the RegValueExists and RegKeyExists functions were written by torgeir, a Microsoft\r\n' MVP in Scripting and WMI. They were made freely available on the Internet.\r\n\r\nDim arrOrphanedPrinters, strComputer, strKeyRoot, strKeyPath, objRegistry, arrSubkeys\r\nDim strSubkey, return, BlnReturn\r\n\r\narrOrphanedPrinters = Array(&quot;iprint&quot;,&quot;printing&quot;)\r\n\r\nConst HKEY_CURRENT_USER = &amp;H80000001\r\nstrComputer = &quot;.&quot;\r\nSet objRegistry = GetObject(&quot;winmgmts:\\\\&quot; &amp; _\r\n strComputer &amp; &quot;\\root\\default:StdRegProv&quot;)\r\n\r\nstrKeyRoot = &quot;HKCU\\&quot;\r\nstrKeyPath = &quot;Printers\\Connections&quot;\r\nIf RegKeyExists(strKeyRoot &amp; strKeyPath) Then\r\n return = objRegistry.EnumKey (HKEY_CURRENT_USER, strKeyPath, arrSubkeys)\r\n If return=0 and IsArray(arrSubkeys) Then\r\n For Each strSubkey In arrSubkeys\r\n BlnReturn=InArray(strSubkey,arrOrphanedPrinters)\r\n If BlnReturn Then\r\n DeleteSubkeys HKEY_CURRENT_USER, strKeyPath &amp; &quot;\\&quot; &amp; strSubkey\r\n End If\r\n Next\r\n End If\r\nEnd If\r\n\r\nstrKeyRoot = &quot;HKCU\\&quot;\r\nstrKeyPath = &quot;Printers\\DevModePerUser&quot;\r\nIf RegKeyExists(strKeyRoot &amp; strKeyPath) Then\r\n' wscript.echo strKeyRoot &amp; strKeyPath &amp; &quot; exists.&quot;\r\n Call DeleteValues(strKeyRoot,strKeyPath)\r\nEnd If\r\n\r\nstrKeyRoot = &quot;HKCU\\&quot;\r\nstrKeyPath = &quot;Printers\\DevModes2&quot;\r\nIf RegKeyExists(strKeyRoot &amp; strKeyPath) Then\r\n' wscript.echo strKeyRoot &amp; strKeyPath &amp; &quot; exists.&quot;\r\n Call DeleteValues(strKeyRoot,strKeyPath)\r\nEnd If\r\n\r\nstrKeyRoot = &quot;HKCU\\&quot;\r\nstrKeyPath = &quot;Printers\\Settings&quot;\r\nIf RegKeyExists(strKeyRoot &amp; strKeyPath) Then\r\n' wscript.echo strKeyRoot &amp; strKeyPath &amp; &quot; exists.&quot;\r\n Call DeleteValues(strKeyRoot,strKeyPath)\r\nEnd If\r\n\r\nSet objRegistry = Nothing\r\n\r\nwscript.quit(0)\r\n\r\nSub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath)\r\n Dim strComputer, objRegistry, arrSubkeys, strSubkey\r\n strComputer = &quot;.&quot;\r\n Set objRegistry = GetObject(&quot;winmgmts:\\\\&quot; &amp; strComputer &amp; &quot;\\root\\default:StdRegProv&quot;)\r\n objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys\r\n If IsArray(arrSubkeys) Then\r\n For Each strSubkey In arrSubkeys\r\n DeleteSubkeys HKEY_CURRENT_USER, strKeyPath &amp; &quot;\\&quot; &amp; strSubkey\r\n Next\r\n End If\r\n objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath\r\n Set objRegistry = Nothing\r\nEnd Sub\r\n\r\nSub DeleteValues(strKeyRoot,strKeyPath)\r\n Dim strComputer, objRegistry, return, i, arrValueNames, arrValueTypes, BlnReturn\r\n strComputer = &quot;.&quot;\r\n Set objRegistry = GetObject(&quot;winmgmts:\\\\&quot; &amp; strComputer &amp; &quot;\\root\\default:StdRegProv&quot;)\r\n return = objRegistry.EnumValues (HKEY_CURRENT_USER, strKeyPath,_\r\n arrValueNames, arrValueTypes)\r\n If return=0 and IsArray(arrValueNames) Then\r\n For i=0 To UBound(arrValueNames)\r\n BlnReturn=InArray(arrValueNames(i),arrOrphanedPrinters)\r\n If BlnReturn Then\r\n objRegistry.DeleteValue HKEY_CURRENT_USER, strKeyPath, arrValueNames(i)\r\n End If\r\n Next\r\n End If\r\n Set objRegistry = Nothing\r\nEnd Sub\r\n\r\nFunction RegValueExists(sRegValue)\r\n' Returns True or False based of the existence of a registry value.\r\n Dim oShell, RegReadReturn\r\n Set oShell = CreateObject(&quot;WScript.Shell&quot;)\r\n RegValueExists = True ' init value\r\n On Error Resume Next\r\n RegReadReturn = oShell.RegRead(sRegValue)\r\n If Err.Number &lt;&gt; 0 Then\r\n RegValueExists = False\r\n End if\r\n On Error Goto 0\r\n Set oShell = Nothing\r\nEnd Function\r\n\r\nFunction RegKeyExists(ByVal sRegKey)\r\n' Returns True or False based on the existence of a registry key.\r\n Dim sDescription, oShell\r\n Set oShell = CreateObject(&quot;WScript.Shell&quot;)\r\n RegKeyExists = True\r\n sRegKey = Trim (sRegKey)\r\n If Not Right(sRegKey, 1) = &quot;\\&quot; Then\r\n sRegKey = sRegKey &amp; &quot;\\&quot;\r\n End If\r\n On Error Resume Next\r\n oShell.RegRead &quot;HKEYNotAKey\\&quot;\r\n sDescription = Replace(Err.Description, &quot;HKEYNotAKey\\&quot;, &quot;&quot;)\r\n Err.Clear\r\n oShell.RegRead sRegKey\r\n RegKeyExists = sDescription &lt;&gt; Replace(Err.Description, sRegKey, &quot;&quot;)\r\n On Error Goto 0\r\n Set oShell = Nothing\r\nEnd Function\r\n\r\nFunction InArray(item,myarray)\r\n Dim i\r\n For i=0 To UBound(myarray) Step 1\r\n If instr(1,item,myarray(i),1) &gt; 0 Then\r\n InArray=True\r\n Exit Function\r\n End If\r\n Next\r\n InArray=False\r\nEnd Function\r\n\r\n<\/pre>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote this script to help a customer clean up old orphaned and stale printer objects from user profiles. This script can safely be executed in each user session and works a treat. However, you may first want to try my script to remove printers the traditional way. Here is the RemoveOrphanedPrinters.vbs\u00a0script.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[5],"tags":[47,45,46],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-scripting","tag-delete-printers","tag-hkcuprinters","tag-hkey_current_userprinters"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":5,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/31\/revisions\/905"}],"wp:attachment":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}