Link to home
Start Free TrialLog in
Avatar of techdrive
techdriveFlag 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
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

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
Avatar of techdrive

ASKER

No error message it displays on the screen. As I stated trying to get this to get to a csv file.
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
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

I guess your advice did not work Dan. Anyone else can assist with this issue.
subsun you are awesome and thanks
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
SOrry that was for subsun I got an error when running your script.
There is a type in my previous comment.. read it as..
Export-csv won’t accept any objects with out property name
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
worked like a charm many thanks again subsun.