+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
Scripts Thread, Deleting reg Keys - getting user Sid in Coding and Web Development; I'm trying to script something to delete printer connections which are stuck in the registry (through some third party software). ...
  1. #1

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    350

    Deleting reg Keys - getting user Sid

    I'm trying to script something to delete printer connections which are stuck in the registry (through some third party software).

    The problem is they are in HKEY_Users\<USER-SID>\Printers\Connections\,,Printername,

    I've looked at reg delete but can't seem to get it to work with wildcards!

    I did start a thread a while back here but its sligtly different this time!
    Last edited by glennda; 18th July 2012 at 03:32 PM.

  2. #2

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    350
    I think I can get the user sid by doing this wmic useraccount get name, sid | findstr %username% - just need to work out a way for doing that in a batch/vbs to then combine with the reg delete

  3. #3

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    What code have you got so far, what scripting language is this in ?

    Is this only for specific users and you are trying to convert SID to username so you know which users to delete the registry keys from, also have you got the printer names or you doing it for any printer connections for a specific user or users ?

    Link below ( obviously you have the wmic code already )

    http://pcsupport.about.com/od/regist...identifier.htm
    Last edited by mac_shinobi; 18th July 2012 at 03:43 PM.

  4. #4


    Join Date
    Jul 2007
    Location
    Rural heck
    Posts
    2,662
    Thank Post
    120
    Thanked 433 Times in 352 Posts
    Rep Power
    126
    If they're the currently logged on user HKEY_Users\<USER-SID> is the same as HKCU\. Could you just add reg delete line to their log on scripts or use GPP.
    Last edited by K.C.Leblanc; 18th July 2012 at 03:43 PM.

  5. #5

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    350
    Quote Originally Posted by K.C.Leblanc View Post
    If they're the currently logged on user HKEY_Users\<USER-SID> is the same as HKCU\
    Thats what I thought - but if you look under HKCU\Printers\Connections there is none - if I look under HKU\<SID>\PRINTERS\connections there is the printers listed - remove these and it keys below and the printers are removed from printers and faxes.

    So Far I know that

    reg delete HKU\<sid>\Printer\Connections\,,servername,printer name works

    and

    wmic useraccount get name, sid | findstr %username%

    outputs
    username <sid>

    Just need a way to combine the two to do what i need!

  6. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    VBS Getting SID for currently logged on user :

    Code:
    'find current user & domain
    Set wshShell = CreateObject("WScript.Shell")
    strUsername = wshShell.ExpandEnvironmentStrings("%USERNAME%")
    strDomain = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
    
    'WScript.Echo "Username: " & strUsername
    'WScript.Echo "Domain: " & strDomain
    
    'use the user/domain information to retrieve the SID of the user and print it to the screen
    WScript.Echo getSid()
    
    Private Function getSid()
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & strUsername & "',Domain='" & strDomain & "'")
    getSID = objAccount.SID
    End Function
    You able to post back a screen grab of registry window where the key(s) are so I can see the registry type ie reg_sz or whatever

    Also are you deleting just one registry key or a registry key and all of its sub keys etc ??

    Am looking at the below link regards to deletion of a registry key

    Posteet: VBScript : How to delete a Windows' registry key and all its subkeys [delete] [registry] [remove] [vbscript] [windows]

    Then just a case of using the getsid() as a variable in the registry key so

    Code:
    'DeleteRegKeyAndSubKeys "HKEY_USERS", "HKU\" & getsid() & "\Printer\Connections\,,servername,printer  name"
    Am not sure if you need to get the servername and printer name via code as well ??

    If it is just the deletion of one key then the code to delete it will be a lot shorter than the link above
    Last edited by mac_shinobi; 18th July 2012 at 04:06 PM.

  7. #7


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,802
    Thank Post
    231
    Thanked 883 Times in 759 Posts
    Rep Power
    300
    can you do it via group policy preferences?

  8. #8

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    350
    @mac_shinobi

    SHW reg keys.png

    Basicly all of the keys under connections.

  9. #9

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    350
    Quote Originally Posted by sted View Post
    can you do it via group policy preferences?
    Its not all users so just want to be able to do it ad-hock.

  10. #10

    AngryTechnician's Avatar
    Join Date
    Oct 2008
    Posts
    3,730
    Thank Post
    698
    Thanked 1,212 Times in 761 Posts
    Rep Power
    395
    Quote Originally Posted by glennda View Post
    Thats what I thought - but if you look under HKCU\Printers\Connections there is none - if I look under HKU\<SID>\PRINTERS\connections there is the printers listed
    This sounds very dodgy indeed. On my account, the printers are listed identically in both places, and HKCU is supposed be be nothing more than a pointer to the correct HKU\SID node.

  11. #11

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Not thoroughly tested but something like :

    Code:
    Const HKEY_CLASSES_ROOT  = &H80000000
    Const HKEY_CURRENT_USER  = &H80000001
    Const HKEY_LOCAL_MACHINE  = &H80000002
    Const HKEY_USERS   = &H80000003
    Const HKEY_CURRENT_CONFIG  = &H80000005
    
    Set wshShell = CreateObject("WScript.Shell")
    strUsername = wshShell.ExpandEnvironmentStrings("%USERNAME%")
    strDomain = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
    
    strComputer = "."
    Set StdOut = WScript.StdOut
     
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
     
    strKeyPath =  Trim(getSid()) & "\Printers\Connections\"
    
    msgbox strKeyPath
    oReg.EnumKey HKEY_USERS, strKeyPath, arrSubKeys
     
    For Each subkey In arrSubKeys
        WshShell.RegDelete "HKEY_USERS\" & strKeyPath & subkey & "\"
    Next
    
    Private Function getSid()
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & strUsername & "',Domain='" & strDomain & "'")
    getSID = objAccount.SID
    End Function
    @glennda - might want to test this on a dummy machine or one that you are not too worried about just to make sure it is doing what it should do. Not done any error checking etc
    Last edited by mac_shinobi; 18th July 2012 at 05:26 PM.

  12. Thanks to mac_shinobi from:

    glennda (18th July 2012)

  13. #12

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    350
    Thanks that works brilliantly!

    At the end end of the script it echo's the registy value - any idea what to remove?

    I havn't got a clue with VB you see!

    EDIT: - removed the obvious message box line!
    Last edited by glennda; 18th July 2012 at 08:27 PM.

  14. Thanks to glennda from:

    mac_shinobi (18th July 2012)

  15. #13

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,730
    Thank Post
    344
    Thanked 520 Times in 488 Posts
    Rep Power
    180
    Quote Originally Posted by glennda View Post
    Thanks that works brilliantly!

    At the end end of the script it echo's the registy value - any idea what to remove?

    I havn't got a clue with VB you see!
    msgbox strKeyPath

    I'm assuming that bit you mean?

    Steve

  16. 2 Thanks to Steve21:

    glennda (18th July 2012), mac_shinobi (18th July 2012)

  17. #14

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    350
    Quote Originally Posted by Steve21 View Post
    msgbox strKeyPath

    I'm assuming that bit you mean?

    Steve
    yeah i noticed that after - edited line above!

  18. #15

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    sorry was in a bit of a rush - also there is the stdout line - I think ( not sure ) but you may be able to remove that as well.


    Set StdOut = WScript.StdOut

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. logoff script to delete registry key
    By ICMC in forum Scripts
    Replies: 3
    Last Post: 26th January 2009, 10:37 PM
  2. CC3, 2Type and Reg Keys
    By CHR1S in forum Network and Classroom Management
    Replies: 2
    Last Post: 25th April 2008, 04:06 PM
  3. Replies: 2
    Last Post: 28th November 2007, 04:40 PM
  4. Mandatory profile gets deleted along with the user account
    By mark_sharman in forum Network and Classroom Management
    Replies: 2
    Last Post: 23rd February 2007, 09:38 PM
  5. script to change permissions on Reg Key
    By e_g_r in forum Windows
    Replies: 4
    Last Post: 16th February 2007, 05:48 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •