How to print all users from windows-group to a textfile?

Posted by Tim on Super User See other posts from Super User or by Tim
Published on 2011-01-10T13:53:37Z Indexed on 2011/01/10 15:56 UTC
Read the original article Hit count: 239

Hello,

i'm trying to print all users of a group "Students" to a Textfile "Students.txt".

I'm not in a domain, so this does not work:

net group "Students" >>  students.txt

because i get following:

This command can be used only on a Windows Domain Controller.

Thank you in advance

If anybody is interested in a VB.Net solution, i've programmed a Winform solution with a multiline Textbox to copy/paste the members (anyway, thanks for your help):

Imports System.DirectoryServices 'first add a refernce to it from .Net Tab'

....

   Private Sub PrintGroupMember_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim students As List(Of DirectoryEntry) = MembersOfGroup("Students")
        For Each user As DirectoryEntry In students
            Me.TextBox1.Text &= user.Name & vbCrLf
        Next
    End Sub

    Public Function MembersOfGroup(ByVal GroupName As String) As List(Of DirectoryEntry)
        Dim members As New List(Of DirectoryEntry)
        Try
            Using search As New DirectoryEntry("WinNT://./" & GroupName & ",group")
                For Each member As Object In DirectCast(search.Invoke("Members"), IEnumerable)
                    Dim memberEntry As New DirectoryEntry(member)
                    members.Add(memberEntry)
                Next
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return members
    End Function

© Super User or respective owner

Related posts about Windows

Related posts about user-accounts