Export all AD users and memberof name (not CN)

I have a PowerShell script which grabs all users from AD and throws out the SamAccountName & their Memberof into two separate columns. I am trying to figure out how I can amend this script so that the Memeberof column only outputs the group name and not the full CN. Can anyone offer some help please? Script:

Get-ADUser -Filter * -Properties memberof| Select samaccountname, @{n='memberOf';e={$_.memberOf -join '; '}} | Export-Csv D:\Reports\groupstest.csv -NTI

Just for some further information, I did have a script that accomplished what I wanted, but for some reason it wasn't exporting all AD users (unlike the above script), there was about 1200 or so records that were missing from the export - the script used for that was this:

Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties SamAccountName,memberof | % {
  New-Object PSObject -Property @{
      UserName = $_.SamAccountName
      Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join ";"
      }
} | Select UserName,Groups | Export-Csv D:\Reports\GroupMemberships.csv -NTI


Any help would be appreciated.

Thanks,
Graham
Graham MooreAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
I don't see any reason why your original code (second listed above) wouldn't work (there's no actual problem with the code).  Perhaps a timeout was happening if the command was taking a long time to complete.
Try something like the below.  It should perform a lot faster at the expense of increased memory.
$groupHash = @{}
Get-ADGroup -Filter * | ForEach `
{
    $groupHash.Add($_.distinguishedName,$_.Name)
}
Get-ADUser -Filter * -Properties SamAccountName,memberof | ForEach `
{
  New-Object PSObject -Property @{
      UserName = $_.SamAccountName
      Groups = ($_.memberof | % { $groupHash[$_] }) -join ";"
      }
} | Select UserName,Groups | Export-Csv D:\Reports\GroupMemberships.csv -NTI

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Graham MooreAuthor Commented:
Thanks for your help, that seems to have done the trick!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.