Solved

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

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

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, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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