Solved

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

Posted on 2015-02-11
4
60 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
[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
  • 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 40

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 40

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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

739 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