Solved

Powershell list group membership all users in a list and export to csv in readable format

Posted on 2015-02-11
4
48 Views
Last Modified: 2015-08-11
Hi
I need to produce a list of group memberships for a list of users and then export the info that is in some sort of legible format.
I have several  lists of users with that I want to pipe in to the script the list are one column containing samaccountname
I am having real trouble doing this and help is much appreciated
0
Comment
Question by:nathantrevor
  • 2
  • 2
4 Comments
 
LVL 13

Assisted Solution

by:Guy Lidbetter
Guy Lidbetter earned 250 total points
ID: 40603032
Hi nathantrevor

Try this

$Users = Import-CSV <FileName>
foreach ($User in $Users) {Get-ADPrincipalGroupMembership $User.SamAccountName | Select @{Label="User";Expression={$User.Samaccountname}},@{Label="Group";Expression={$_.name}}} | Export-CSV <OutFile> - NoTypeInformation

Regards

Guy
0
 
LVL 39

Accepted Solution

by:
footech earned 250 total points
ID: 40603706
Only problem I see with the above is that you can't pipe from the foreach statement (but you can with the ForEach-Object cmdlet).
Assuming there's no header in the file, here's what I'd do.
Get-Content file.txt | ForEach `
{
   $user = $_
   Get-ADPrincipalGroupMembership $user | Select @{n="User";e={$user}},@{n="Group";e={$_.name}}
} | Export-CSV memberships.csv -notype

Open in new window

0
 
LVL 13

Expert Comment

by:Guy Lidbetter
ID: 40603764
Ah, yes footech, I see what you mean. I assumed he was using a CSV file, your way is better with a txt file.
0
 
LVL 39

Expert Comment

by:footech
ID: 40603924
If there is a header, we could use Import-CSV.  Would just need to adjust lines 1 and 3.
0

Join & Write a Comment

Resolve DNS query failed errors for Exchange
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

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

17 Experts available now in Live!

Get 1:1 Help Now