Windows Groups and VBScript

posted @ Friday, October 15, 2004 1:06 PM

 

Someone at work recently came up to me and asked if I would write up a quick VBScript file to display in a message box the list of Windows Groups a user is assigned to if the user ran it locally. This is what I came up with:

Dim WshShell, objDomain, objUserName, list, oGroup

Set WshShell = WScript.CreateObject("WScript.Shell")

objDomain = WshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
objUserName = WshShell.ExpandEnvironmentStrings("%USERNAME%")

Set objUser = GetObject("WinNT://" & objDomain & "/" & objUserName & "")

On Error Resume Next
list = objUserName & " belongs to the following groups:" & vbCrLf & vbCrLf
For Each oGroup In objUser.Groups
list = list & vbCrLf & oGroup.Name
Next
Set WshShell = Nothing
MsgBox list