Solved

powershell get a list of users membership to a group

Posted on 2013-12-02
11
1,610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
11 Comments
 
LVL 35

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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