locked
Output AD Users with associated groups RRS feed

  • Question

  • Hello everyone!!

    I am racking my brain with this issue I am having! I am hoping that someone here will be able to help me out. 

    I am trying to poll active directory for a set of user accounts and output their name with all the associated groups. I have been trying a couple different things but the output is not what I am expecting. I either get length, or System.Object[], the lastest I get two rows in excel with all my data in one string. The Ideal output would be to have it in a CSV or text file as two columns.

    Here are the codes I am working with 

    $search = "MY PATH"
    $csvpath = "C:\Users\WHY\Desktop\AD\ADUsers.csv"
    
    $user = get-aduser -Filter * -SearchBase $search -properties Name,memberof 
    
    $groups = Foreach ($NewUser in $user){(Get-ADPrincipalGroupMembership -Identity "$NewUser" |select-object -ExpandProperty name) -join ','} 
    
    $Name = $user.Name
    $memberOf = $groups
    
    $output = "$Name","$memberOf"
    $output | out-file $csvpath

    I have also tried this

    $search = "MY PATH"
    $csvpath = "C:\Users\WHY\Desktop\AD\ADUsers.csv"
    
    $user = get-aduser -Filter * -SearchBase $search -properties Name,memberof,givenname,surname 
    
    $groups = Foreach ($NewUser in $user){(Get-ADPrincipalGroupMembership -Identity "$NewUser" |select-object -ExpandProperty name) -join ','} 
    
    $output = [PSCustomObject]@{ 
              Name = $user.Name
              FirstName = $user.givenname
              LastName = $user.surname
              memberOf = $groups
              }
    $output | out-file $csvpat

    Any help would be much appriciated, I have been doing a lot of googling and trying different things before I decided to post here. 

    Thank you for your time.

    Tuesday, December 11, 2018 11:59 PM

Answers

  • Hi,

    Thanks for your question.

    $csvpath = "C:\Users\administrator\Desktop\ADUsers1.csv"
    $user = get-aduser -Filter * -properties Name,memberof,givenname,surname 
     Foreach ($NewUser in $user) {
       $obj+=Get-ADPrincipalGroupMembership -Identity $NewUser |
            select-object @{name="username";expression={$NewUser.Name}},@{name="firstname";expression={$NewUser.GivenName}},@{name="lastname";expression={$NewUser.Surname}},@{name="memberof";expression={$_.name}} 
    }
    $obj| Export-Csv $csvpath
    

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by JQuinlan Wednesday, December 12, 2018 3:33 PM
    Wednesday, December 12, 2018 5:59 AM

All replies

  • You have to use "Export-Csv" and not Out-File.

    help export-csv -online


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Wednesday, December 12, 2018 1:13 AM
    • Unproposed as answer by JQuinlan Wednesday, December 12, 2018 2:36 AM
    • Proposed as answer by BOfH-666 Wednesday, December 12, 2018 3:19 AM
    Wednesday, December 12, 2018 12:46 AM
  • I tried export-csv. But it only gives me string lengths in values, or no information such as System.Object[]
    Wednesday, December 12, 2018 1:33 AM
  • You cannot output or export collections.  You need to select simple properties to export to t CSV.


    \_(ツ)_/

    Wednesday, December 12, 2018 1:36 AM
  • Here is a basic example to start with:

    Get-AdUser -Filter * -SearchBase $search -properties Name, memberof, givenname, surname |
        Select Name, GivenName, Surname |
        Export-Csv file.csv -NoType

    To export all group membership look in the Gallery for numerous scripts that do this.


    \_(ツ)_/

    Wednesday, December 12, 2018 1:40 AM
  • I do not want members of a group I want groups the members are in. I understand what you are saying. I just do not understand why in my second code above, the out-file will not output the data, but I can do it separately and would have to combine the items. 
    Wednesday, December 12, 2018 2:27 AM
  • Because your code does not do what you expect.  It is trying to turn a collection into an object.

    If you learn some basic PowerShell and format your code so it is readable you can easily see your mistakes:

    $search = 'MY PATH'
    $csvpath = 'C:\Users\WHY\Desktop\AD\ADUsers.csv'
    
    $users = get-aduser -Filter * -SearchBase $search -properties Name, memberof, givenname, surname
    
    $groups = Foreach ($NewUser in $users) {
        (Get-ADPrincipalGroupMembership -Identity $NewUser |
            select-object -ExpandProperty name) -join ','
    }
    
    # the folowing only is the last user in the above loop
    $output = [PSCustomObject]@{
        Name = $user.Name
        FirstName = $user.givenname
        LastName = $user.surname
        memberOf = $groups  #<<<<==== all groups for all users
    }
    
    $output | out-file $csvpath

    Also "Out-File" cannot create a CSV.


    \_(ツ)_/

    Wednesday, December 12, 2018 5:02 AM
  • Hi,

    Thanks for your question.

    $csvpath = "C:\Users\administrator\Desktop\ADUsers1.csv"
    $user = get-aduser -Filter * -properties Name,memberof,givenname,surname 
     Foreach ($NewUser in $user) {
       $obj+=Get-ADPrincipalGroupMembership -Identity $NewUser |
            select-object @{name="username";expression={$NewUser.Name}},@{name="firstname";expression={$NewUser.GivenName}},@{name="lastname";expression={$NewUser.Surname}},@{name="memberof";expression={$_.name}} 
    }
    $obj| Export-Csv $csvpath
    

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by JQuinlan Wednesday, December 12, 2018 3:33 PM
    Wednesday, December 12, 2018 5:59 AM
  • Lee,

    Thank you for the assistance. I can see where I was going wrong based on your example. 

    Wednesday, December 12, 2018 3:34 PM