Avatar of techdrive
techdrive
Flag for United States of America asked on

powershell get a list of users membership to a group

I have been having a tough time. All I am trying to do is get a list of users and all the groups they belong to in a csv file. Please help here is my code.

Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=contoso users,OU=SBG,DC=contoso,DC=com" -Filter * | foreach-object {
write-host "User:" $_.Name -foreground green
    Get-ADPrincipalGroupMembership $_.SamAccountName | foreach-object {
        write-host "Member Of:" $_.name
    }
} | out-file c:\temp\listall.csv
Powershell

Avatar of undefined
Last Comment
techdrive

8/22/2022 - Mon
Dan Craciun

What exactly is the problem? Error messages, incomplete/erroneous data?

Anyway, if you want to export to csv it's better to use Export-Csv instead of Out-File.

And if you're printing to a text file, I would use echo instead of write-host.

HTH,
Dan
techdrive

ASKER
No error message it displays on the screen. As I stated trying to get this to get to a csv file.
techdrive

ASKER
I placed an echo instead of write host and my output is numbers. Have you tested your solution Dan.

Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=contoso users,OU=SBG,DC=contoso,DC=com" -Filter * | foreach-object {
echo "User:" $_.Name
    Get-ADPrincipalGroupMembership $_.SamAccountName | foreach-object {
      echo "Member Of:" $_.name
    }
} | export-csv -notypeinformation c:\temp\listall.csv
Your help has saved me hundreds of hours of internet surfing.
fblack61
SubSun

First example you have used write-host, which will not pass any object to pipe line, so it won’t work. Second example you have used Echo with Export-csv. echo will pass the objects to powershell pipeline however Export-csv won’t accept any objects with property name (echo with out-file may work). Try this and see if it works for you..
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=contoso users,OU=SBG,DC=contoso,DC=com" -Filter * | foreach-object {
$User = $_.Name 
 Get-ADPrincipalGroupMembership $_.SamAccountName | Select @{N="User";e={$User}},@{N="User";e={$_.Name}}
} | export-csv -notypeinformation c:\temp\listall.csv

Open in new window

techdrive

ASKER
I guess your advice did not work Dan. Anyone else can assist with this issue.
techdrive

ASKER
subsun you are awesome and thanks
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
techdrive

ASKER
I got an error when running this

Select-Object : Property cannot be processed because property "User" already ex
ists.
At line:1 char:154
+ Get-ADUser -SearchBase "OU=contoso users,OU=SBG,DC=contoso,DC=com" -Filter * |
 foreach-object {Get-ADPrincipalGroupMembership $_.SamAccountName } | select <<
<<  @{N="User";e={$User}},@{N="User";e={$_.Name}}
    + CategoryInfo          : InvalidOperation: (CN=
   =int:PSObject) [Select-Object], PSArgumentException
    + FullyQualifiedErrorId : AlreadyExistingUserSpecifiedPropertyNoExpand,Mic
   rosoft.PowerShell.Commands.SelectObjectCommand
techdrive

ASKER
SOrry that was for subsun I got an error when running your script.
SubSun

There is a type in my previous comment.. read it as..
Export-csv won’t accept any objects with out property name
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
techdrive

ASKER
worked like a charm many thanks again subsun.