techdrive
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-ADPrincipalGroupMember ship $_.SamAccountName | foreach-object {
write-host "Member Of:" $_.name
}
} | out-file c:\temp\listall.csv
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=contoso users,OU=SBG,DC=contoso,DC
write-host "User:" $_.Name -foreground green
Get-ADPrincipalGroupMember
write-host "Member Of:" $_.name
}
} | out-file c:\temp\listall.csv
ASKER
No error message it displays on the screen. As I stated trying to get this to get to a csv file.
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-ADPrincipalGroupMember ship $_.SamAccountName | foreach-object {
echo "Member Of:" $_.name
}
} | export-csv -notypeinformation c:\temp\listall.csv
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=contoso users,OU=SBG,DC=contoso,DC
echo "User:" $_.Name
Get-ADPrincipalGroupMember
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
ASKER
I guess your advice did not work Dan. Anyone else can assist with this issue.
ASKER
subsun you are awesome and thanks
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-ADPrincipalGroupMembe rship $_.SamAccountName } | select <<
<< @{N="User";e={$User}},@{N= "User";e={ $_.Name}}
+ CategoryInfo : InvalidOperation: (CN=
=int:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : AlreadyExistingUserSpecifi edProperty NoExpand,M ic
rosoft.PowerShell.Commands .SelectObj ectCommand
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
foreach-object {Get-ADPrincipalGroupMembe
<< @{N="User";e={$User}},@{N=
+ CategoryInfo : InvalidOperation: (CN=
=int:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : AlreadyExistingUserSpecifi
rosoft.PowerShell.Commands
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
worked like a charm many thanks again subsun.
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