Solved

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

Posted on 2015-02-11
4
52 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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
A brief introduction to what I consider to be the best editor for PowerShell.
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

920 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

16 Experts available now in Live!

Get 1:1 Help Now