VBS Script – Active Directory User Group Membership Checker – memberOf

Here is a very useful VBS function; I use it a lot in logon scripts.

The function will return True or False depending on if the current user running the script is a member of the AD group passed to the function.

To get the function working you will need to replace domain_name_here in the function with you active directory domain name

This particular script will not work with nested groups.

Example Usage:

If memberOf("GroupName") then
'*** Do some stuff *** 
End If

The Function:

Function memberOf(groupName)
 set oNet = CreateObject("WScript.Network")
 set oUser = GetObject("WinNT://domain_name_here/" & oNet.UserName & ",user")

 ' Enurmerate Groups for the user
 for each oGroup in oUser.Groups
  if ucase(oGroup.Name) = ucase(groupName) then
   memberOf = true
   exit for
  end if
 next

 set oUser = nothing
 set oGroup = nothing
 set onet = nothing
End function

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.