locked
Powershell to extract AD users MemberOf RRS feed

  • Question

  • Any expert can help on this? I will want to extract all AD users memberof fields and export to text file filename based on their username

    Example:

    Jack Ong.txt -> open up will show his memberof:-

    Domain Admins

    Accounting

    VPN User

    Thursday, October 31, 2019 7:36 AM

Answers

  • Hi sgjack82, 

    Here's an example with PowerShell.

    You need to change the -SearchBase so it match your AD-structure and domain. You probably want to change the out-file path also. 

    # Finds all users in a specific OU
    $Users = Get-ADUser -SearchBase "ou=myusers,DC=playground,DC=se" -filter *
    
    # For each user get their group memberships and send the information to a txt-file
    foreach ($user in $users) {
     (Get-ADPrincipalGroupMembership -identity ($user).distinguishedName).name | out-file ('C:\folder\' + ($user).name + '.txt') -Force
    }


    Best Regards, 

    Mats Haby


    Thursday, October 31, 2019 8:00 AM
  • hello,

    If you want that each txt have the group name  and each txt have the content of the group:

    $groups = get-adgroup -filter *
    
    foreach ($group in $groups) 
    {
     $users=Get-ADGroupMember -identity $group.name 
     $logname =[string]"C:\script\"+$group.name+".txt"
     $users.name | Out-File -filepath $logname
     }

    If this is the opposite , the Mats's script is perfect

    Thursday, October 31, 2019 1:17 PM

All replies

  • Please carefully review the following links to set your expectation for posting in technical forums.

    You can find pre-written scrips here: Microsoft  Script Gallery


    \_(ツ)_/

    Thursday, October 31, 2019 7:40 AM
  • Hi sgjack82, 

    Here's an example with PowerShell.

    You need to change the -SearchBase so it match your AD-structure and domain. You probably want to change the out-file path also. 

    # Finds all users in a specific OU
    $Users = Get-ADUser -SearchBase "ou=myusers,DC=playground,DC=se" -filter *
    
    # For each user get their group memberships and send the information to a txt-file
    foreach ($user in $users) {
     (Get-ADPrincipalGroupMembership -identity ($user).distinguishedName).name | out-file ('C:\folder\' + ($user).name + '.txt') -Force
    }


    Best Regards, 

    Mats Haby


    Thursday, October 31, 2019 8:00 AM
  • hello,

    If you want that each txt have the group name  and each txt have the content of the group:

    $groups = get-adgroup -filter *
    
    foreach ($group in $groups) 
    {
     $users=Get-ADGroupMember -identity $group.name 
     $logname =[string]"C:\script\"+$group.name+".txt"
     $users.name | Out-File -filepath $logname
     }

    If this is the opposite , the Mats's script is perfect

    Thursday, October 31, 2019 1:17 PM
  • Hello

    do you find a solution on this thread ? if yes, could you close this with "mark as anwser"?

    Ty

    Olivier

    Tuesday, November 19, 2019 11:30 AM