Solved

powershell get a list of users membership to a group

Posted on 2013-12-02
11
1,446 Views
Last Modified: 2013-12-02
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
0
Comment
Question by:techdrive
  • 7
  • 3
11 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39689895
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
0
 

Author Comment

by:techdrive
ID: 39689899
No error message it displays on the screen. As I stated trying to get this to get to a csv file.
0
 

Author Comment

by:techdrive
ID: 39689925
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
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39689984
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

0
 

Author Comment

by:techdrive
ID: 39690001
I guess your advice did not work Dan. Anyone else can assist with this issue.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:techdrive
ID: 39690003
subsun you are awesome and thanks
0
 

Author Comment

by:techdrive
ID: 39690022
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
0
 

Author Comment

by:techdrive
ID: 39690025
SOrry that was for subsun I got an error when running your script.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39690026
There is a type in my previous comment.. read it as..
Export-csv won’t accept any objects with out property name
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39690034
Sorry.. change line 4 to..
Get-ADPrincipalGroupMembership $_.SamAccountName | Select @{N="User";e={$User}},@{N="Member";e={$_.Name}}

Open in new window

0
 

Author Comment

by:techdrive
ID: 39690172
worked like a charm many thanks again subsun.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now