locked
formatting output question for a Active Directory Moduel for Powershell script RRS feed

  • Question

  • I wrote a simple script that uses the AD Module for PowerShell  that uses the Get-ADGroup and Get-ADGroupMember commandlets to grab all security groups in the domain and then list all the members for each group. Could someone please help me with some ways to format the output to make it more readable? Ideally I'd like to get it formatted in a spreadsheet where the top row each column is the group name and below each group is a list of the members going down the rows.

    when I ran this script the output on the screen shows all of the groups and members. See script below that I would like some assistance with the output formatting.

    Script:

    $secgrps = Get-ADGroup -Filter 'GroupCategory -eq "Security"'
        foreach ($secgrp in $secgrps)
            {
                get-adgroupmember -identity $secgrp.DistinguishedName
            }

    • Changed type Bill_Stewart Tuesday, September 27, 2011 7:14 PM This is a question
    Tuesday, September 27, 2011 7:08 PM

Answers

  • Hi,

    Sorry, I forgot to make the foreach loop a scriptblock (indented for clarity):

    $secGroups = get-adgroup -filter 'GroupCategory -eq "Security"' |
      select-object -expandproperty DistinguishedName |
      sort-object
    & {
        foreach ($secGroup in $secGroups) {
          $members = get-adgroupmember -identity $secGroup |
            select-object -expandproperty DistinguishedName |
            sort-object
          foreach ($member in $members) {
              "" | select-object @{Name="Group"; Expr={$secGroup}},
                @{Name="Member"; Expr={$member}}
          }
        }
    } | export-csv "GroupsAndMembers.csv" -notypeinformation
    
    

    HTH,

    Bill


    • Edited by Bill_Stewart Tuesday, September 27, 2011 8:35 PM indent correction
    • Marked as answer by mporzio Wednesday, September 28, 2011 12:53 PM
    Tuesday, September 27, 2011 8:34 PM

All replies

  • Hi,

    You could do something like this:

    $secGroups = get-adgroup -filter 'GroupCategory -eq "Security"' |
    select-object -expandproperty DistinguishedName |
    sort-object
    foreach ($secGroup in $secGroups) {
    $members = get-adgroupmember -identity $secGroup |
    select-object -expandproperty DistinguishedName |
    sort-object
    foreach ($member in $members) {
    "" | select-object @{Name="Group"; Expr={$secGroup}},
    @{Name="Member"; Expr={$member}}
    }
    } | export-csv "GroupsAndMembers.csv" -notypeinformation

    This outputs groups and members to a CSV file.

    HTH,

    Bill

    Tuesday, September 27, 2011 7:28 PM
  • when I do that I get an error listed below. Also I wanted to keep it as readable as possible in a format like:

     

    Group 1                              Group 2                           Group 3

    user1                                 user1                              

    user2                                 nested group 1

    nested group 1                 user2

    user 3                               

     

    Error I recieved when running the above script:

    PS C:\Scripts> .\test3.ps1
    An empty pipe element is not allowed.
    At C:\Scripts\test3.ps1:12 char:4
    + } | <<<<  export-csv "GroupsAndMembers.csv" -notypeinformation
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : EmptyPipeElement

    Tuesday, September 27, 2011 7:46 PM
  • Hi,

    You might have one or more groups with no members?

    To get that column layout, you would need to restructure your custom objects. I don't recommend it as a single user might be a member of a large number of groups, meaning you will have an unwieldy number of columns. If you keep the format simple, you can restructure it later in a spreadsheet.

    Bill

    Tuesday, September 27, 2011 8:05 PM
  • ok... I can do that and expand upon this later. I modified the script a little to give me just the names instead of DN but I still get the error when trying to export to csv. Below is the modified script, works with screen output when I take out the last line but I get the same error as above when running it with the export-csv command. the error is below the modified script. Thanks.

     

    $secGroups = get-adgroup -filter 'GroupCategory -eq "Security"'
      sort-object
    foreach ($secGroup in $secGroups) {
        $members = get-adgroupmember -identity $secGroup.distinguishedname |
          select-object Name |
          sort-object
        foreach ($member in $members) {
            "" | select-object @{Name="Group"; Expr={$secGroup.name}},
              @{Name="Member"; Expr={$member.name}}
        }
    } | export-csv "GroupsAndMembers.csv" -notypeinformation

     

     

     

    PS C:\Scripts> .\test3.ps1
    An empty pipe element is not allowed.
    At C:\Scripts\test3.ps1:12 char:4
    + } | <<<<  export-csv "GroupsAndMembers.csv" -notypeinformation
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : EmptyPipeElement

     

     

    Tuesday, September 27, 2011 8:10 PM
  • Hi,

    Run it without the

     | export-csv "GroupsAndMembers.csv" -notypeinformation

    on the last line and see where it stops.

    Bill

    Tuesday, September 27, 2011 8:19 PM
  • it runs perfectly fine with everything outputting to the screen when I take that line out, no errors.

    the problem comes when I have that line in and try to export it to csv.

    Tuesday, September 27, 2011 8:26 PM
  • Hi,

    Sorry, I forgot to make the foreach loop a scriptblock (indented for clarity):

    $secGroups = get-adgroup -filter 'GroupCategory -eq "Security"' |
      select-object -expandproperty DistinguishedName |
      sort-object
    & {
        foreach ($secGroup in $secGroups) {
          $members = get-adgroupmember -identity $secGroup |
            select-object -expandproperty DistinguishedName |
            sort-object
          foreach ($member in $members) {
              "" | select-object @{Name="Group"; Expr={$secGroup}},
                @{Name="Member"; Expr={$member}}
          }
        }
    } | export-csv "GroupsAndMembers.csv" -notypeinformation
    
    

    HTH,

    Bill


    • Edited by Bill_Stewart Tuesday, September 27, 2011 8:35 PM indent correction
    • Marked as answer by mporzio Wednesday, September 28, 2011 12:53 PM
    Tuesday, September 27, 2011 8:34 PM
  • Thanks!!
    Wednesday, September 28, 2011 12:53 PM