Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell get a list of users membership to a group

Posted on 2013-12-02
11
Medium Priority
?
1,758 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 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 2000 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

WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

Question has a verified solution.

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

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
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…
Loops Section Overview

971 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