none
Extract Members of "Selected AD Groups" :: Input: CSV File :: OUTPUT: CSV File RRS feed

  • Question

  • Dear Leaders,

    I am looking for a script to extracts AD Group Members (sourced from CSV/TXT file) and Output to CSV/TXT file.

    Can someone help me with this customized script.

    In particular I am looking for a script which generates SINGLE OUTPUT file in following format.

    GroupName    GroupMembers
    ----------------------------
    ADGroup1    Member1,Member2,Member3
    ADGroup2    Member1,Member2,Member3
    ADGroup3    Member1,Member2,Member3

    Thank You Very much in advance !!

    Regards,
    Amit Kumar Rao
    Tuesday, July 29, 2014 1:22 AM

All replies

  • What is the format of the source file? Like this?

    GroupName    GroupMember
    ----------------------------
    ADGroup1     Member1
    ADGroup1     Member2
    ADGroup1     Member3
    ADGroup2     Member1
    ADGroup2     Member2
    ADGroup2     Member3
    ADGroup3     Member1
    ADGroup3     Member2
    Adgroup3     Member3

    Is it in order like this?

    Tuesday, July 29, 2014 1:29 AM
  • Hello Brian,

    Source File has Security Groups listed as in below format:

    GroupName

    ----------------

    ADGroup1

    ADGroup2

    ADGroup3

    .

    .

    ADGroupN

    However if you suggest a suitable script then I can manipulate the source file as per your suggestion.

    Regards,

    Amit Kumar Rao

    Tuesday, July 29, 2014 2:14 AM
  • Hi,

    Here's how I'd do something like that (this gets all security groups from AD instead of using an input text file):

    Get-ADGroup -Filter "GroupCategory -eq 'Security'" | ForEach {
    
        $groupName = $_.Name
    
        $props = @{
            GroupName = $groupName
            GroupMembers = ''
        }
    
        try {
    
            $groupList = Get-ADGroupMember -Identity $_.Name -ErrorAction Stop | Select -ExpandProperty SamAccountName | Sort
    
            $props.GroupMembers = $groupList -join ','
    
        } catch {
    
            $props.GroupMembers = 'ERROR getting group membership'
    
        }
    
        New-Object PsObject -Property $props
    
    } | Sort GroupName | Select GroupName,GroupMembers | Export-Csv .\groupMemberships.csv -NoTypeInformation

    Also, I'd recommend posting any additional questions you have in the general PowerShell forum here:

    http://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=winserverpowershell&filter=alltypes&sort=lastpostdesc

    This forum is focused on using PowerShell with DPM.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Tuesday, July 29, 2014 2:34 AM
  • Thanks Mike, I guess as an alternate I can utilize the your script.

    Can you please suggest if I need to target a particular OU then how do i do that..

    Regards,

    Amit Kumar Rao

    Tuesday, July 29, 2014 3:17 AM
  • You can use the -SearchBase parameter to target a particular OU:

    http://technet.microsoft.com/en-us/library/ee617196.aspx

    Alternatively, this is the same script as above, but should be able to use your input file (assuming the group names start on line 3):

    Get-Content .\groupList.txt | Select -Skip 2 | ForEach {
    
        $groupName = $_
    
        $props = @{
            GroupName = $groupName
            GroupMembers = ''
        }
    
        try {
    
            $groupList = Get-ADGroupMember -Identity $groupName -ErrorAction Stop | Select -ExpandProperty SamAccountName | Sort
    
            $props.GroupMembers = $groupList -join ','
    
        } catch {
    
            $props.GroupMembers = 'ERROR getting group membership'
    
        }
    
        New-Object PsObject -Property $props
    
    } | Sort GroupName | Select GroupName,GroupMembers | Export-Csv .\groupMemberships.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Tuesday, July 29, 2014 4:16 AM
  • Mike I am very new to Powershell and I do not know where to make what change.. Can you revise the script as per requirement please.

    Regards,

    Amit Kumar Rao

    Tuesday, July 29, 2014 4:59 AM
  • Get-ADGroup -Filter "GroupCategory -eq 'Security'" -SearchBase "OU=Organization-Unit"

    For Help:

    Get-Help Get-ADGroup -Examples

    if examples not there

    Update-Help

    Tuesday, July 29, 2014 5:06 AM
  • Thanks Brian and Mike. I will check and share the outcome..

    Regards,

    Amit Kumar Rao

    Tuesday, July 29, 2014 5:25 AM