none
expand members array for get-adgroup to a single row seperated by semicolon RRS feed

  • Question


  • I am trying to get the members of a list of groups from a csv. The script i created below outs puts the group names but wont output the members array to a single row. How do specify the members in a single row seperated by a delimiter such as a semicolon?

    get-content "C:\Scripts\Retrieve members of a group\glist.csv" | foreach {Get-ADGroup $_ | select members, name} | Export-CSV 'C:\temp\membersof.csv

    example

    group1       user1; user2; user3; user4

    group2       user1; user2

    Wednesday, January 15, 2014 9:28 PM

Answers

  • i found out how to expand it using a in a single line which expands the property to display on one row in excel. here is the final script

    get-content glist.csv | foreach {get-adgroup -identity "$_" -properties * | select Name,Description, @{n='Members';e={$_.members -replace '^cn=([^,]+).+$','$1' -join '; '}} | export-csv "group_info $currentdate.csv" -NoTypeInformation

    • Marked as answer by glacket Thursday, January 16, 2014 5:16 PM
    Thursday, January 16, 2014 5:16 PM

All replies

  • Expand an array

    $a=1,2,3,4,5,6,7,8
    "$a"

    [string]::Join('|',$a)


    ¯\_(ツ)_/¯

    Wednesday, January 15, 2014 10:11 PM
  • I didn't test this yet, but I'm 95% sure this will get you what you want.

    $output = new-item output.txt $input = import-csv "c:\scripts\retrieve members of a group\glist.csv" | foreach-object ({

    $string = $_ + ":"

    (get-adgroupmember $_ -recursive).name | % ({ $string += $_ + ";" })

    $string | add-content $output })

    It'll read in the input file, and for each name, start a string with the group name.

    Then, it'll get the group members of the group and add them one at a time to the string with the semi-colon.

    Finally, after each group, it'll dump the string to a text file, blank out the string and continue.

    Please let me know how this works out.

     ----- If you find my post to be helpful ( or the answer) Please mark it appropriately.


    Chris Ream




    Thursday, January 16, 2014 3:44 AM
  • Chris, when i run your script it asks type. I am not sure why its generating a question.
    Thursday, January 16, 2014 4:57 PM
  • Are you sure you are asking the correct question.  It does not look like your file is a CSV file on input.  Members are the DNs of the user or group.

    This is the solution to your actual question.

    get-content "C:\Scripts\Retrieve members of a group\glist.csv" | 
         foreach-Object{
              Get-ADGroup $_ | select name,@{N='Members';E={[string]::Join('|',$($_.member))}}
          } | 
          Export-CSV 'C:\temp\membersof.csv
     


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, January 16, 2014 5:13 PM
    Thursday, January 16, 2014 5:06 PM
  • i found out how to expand it using a in a single line which expands the property to display on one row in excel. here is the final script

    get-content glist.csv | foreach {get-adgroup -identity "$_" -properties * | select Name,Description, @{n='Members';e={$_.members -replace '^cn=([^,]+).+$','$1' -join '; '}} | export-csv "group_info $currentdate.csv" -NoTypeInformation

    • Marked as answer by glacket Thursday, January 16, 2014 5:16 PM
    Thursday, January 16, 2014 5:16 PM