VBScript – Check if current user is a member of a certain group

I found some code to do this out on the net the other day. I’ve modified it a little, and added the part that checks environment variables.

Note: 11th October 2010 – Since this post is so popular, I’ve cleaned up the code a bit and re-posted it below.

'Error Handling
On Error Resume Next
set objShell = WScript.CreateObject( "WScript.Shell" )

'Calls the isMember function with the specified group to see if the current user
' is a member of that group.
If isMember("GroupNameToCheckGoesHere") Then
       'MsgBox("Is member") ' Do something here if they are a member of the group
    Else
       'MsgBox("Is not member") ' Do something here if they are not a member of the group
End If

' *****************************************************
'This function checks to see if the passed group name contains the current
' user as a member. Returns True or False
Function IsMember(groupName)
    If IsEmpty(groupListD) then
        Set groupListD = CreateObject("Scripting.Dictionary")
        groupListD.CompareMode = TextCompare
        ADSPath = EnvString("userdomain") & "/" & EnvString("username")
        Set userPath = GetObject("WinNT://" & ADSPath & ",user")
        For Each listGroup in userPath.Groups
            groupListD.Add listGroup.Name, "-"
        Next
    End if
    IsMember = CBool(groupListD.Exists(groupName))
End Function
' *****************************************************

' *****************************************************
'This function returns a particular environment variable's value.
' for example, if you use EnvString("username"), it would return
' the value of %username%.
Function EnvString(variable)
    variable = "%" & variable & "%"
    EnvString = objShell.ExpandEnvironmentStrings(variable)
End Function
' *****************************************************

' Clean up
Set objShell = Nothing
This entry was posted in Scripting, SysAdmin, Windows. Bookmark the permalink.

8 Responses to VBScript – Check if current user is a member of a certain group

  1. chrisNo Gravatar says:

    Excellent script, this is exactly what i was looking for. I appreciate your clean scripting style.

    I will call this in a terminal services logon script to map a drive for only users in a specific group.

    Thanks!

  2. mikeNo Gravatar says:

    I am a bit of a VBScript noob. Here is my question. I am not seeing what the user variable is. Or can this script read all of the Active Directory??

  3. DanielNo Gravatar says:

    It’s just grabbing the username out of an environment variable: EnvString("username")

  4. DemianNo Gravatar says:

    Great script !!!
    Thanks for sharing.

  5. rolandNo Gravatar says:

    Thanks for the great utility, however as this is vbscript you should either define TextCompare in
    groupListD.CompareMode = TextCompare
    or write
    groupListD.CompareMode = 1

    otherwise it would do BinaryCompare (without option explicit turned on) or throw an error…

    -roland

  6. DanielNo Gravatar says:

    Thanks. I’m already using that comparemode

  7. Mircea GaitanNo Gravatar says:

    Hi Daniel,

    Very nice script. How about if I want to find out if the all domain users are members of a certain group?

    Thanks.

    Mircea

  8. DanielNo Gravatar says:

    I haven’t needed to do that in VBScript – but I used to use IFMember.exe in batch files. Apparently it doesn’t work anymore, but I haven’t used it for a long time.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>