0

I'm trying to export the username and the user's group membership (of specifc groups) to a CSV file using Export-Csv. However, I couldn't accomplish this using several approaches.

My current script works fine but the output is shown on the PowerShell console alone:

$accounts = Get-Content "C:\Scripts\getUserGroups\users.txt"
foreach ($account in $accounts) {
    "$account member of:"
    Get-ADPrincipalGroupMembership -Identity $account |
        select Name |
        Where-Object { $_.name -like 'Browsing_Group*' } |
        Sort Name
}

I want to export it to a file in an ordered manner:

UserName1
group membership

UserName2
group membership

etc...

I've tried to add to a variable but probably didn't do that correctly:

$ArrList = [System.Collections.ArrayList]@()
$accounts = Get-Content "C:\Scripts\getUserGroups\users.txt"

foreach ($account in $accounts) {
    $ArrList.Add($account)
    $groups = Get-ADPrincipalGroupMembership -Identity $account |
              select Name |
              Where-Object {$_.name -like 'Browsing_group*' } |
              Sort Name 
    $ArrList.Add($grops)
} 

Might be a different approach.

2
  • Do you want it as a csv-table or a file with a list like in your second code block? Commented Jul 24, 2019 at 8:26
  • I would like to export the received output to a csv file Commented Jul 24, 2019 at 8:35

1 Answer 1

1

You need to build custom objects in order to export the data to a CSV via Export-Csv. The 2 main ways of doing that are:

  • using calculated properties:

    $accounts |
        Select-Object @{n='Username';e={$_}}, @{n='Groups';e={
            (Get-ADPrincipalGroupMembership -Identity $_ |
                Select-Object -Expand Name |
                Where-Object {$_ -like 'Browsing_group*' } |
                Sort-Object) -join ';'
        }} |
        Export-Csv 'C:\path\to\output.csv' -NoType
    
  • building custom objects directly:

    $accounts | ForEach-Object {
        $groups = Get-ADPrincipalGroupMembership -Identity $_ |
                  Select-Object -Expand Name |
                  Where-Object {$_ -like 'Browsing_group*' } |
                  Sort-Object
        New-Object -Type PSObject -Property @{
            'Username' = $_
            'Groups'   = $groups -join ';'
        }
    } | Export-Csv 'C:\path\to\output.csv' -NoType
    

    With PowerShell version 3 or newer you can replace New-Object with the [PSCustomObject] type accelerator:

    [PSCustomObject]@{
        'Username' = $_
        'Groups'   = $groups -join ';'
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

works perfectly! thank you Ansgar Wiechers. Im just trying to understand your code. how come you did not use a foreach loop in the first solution? also, is this @{n='Username';e={$_}} a hash table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.