Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 78
  • Last Modified:

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

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
nathantrevor
Asked:
nathantrevor
  • 2
  • 2
2 Solutions
 
Guy LidbetterCommented:
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
 
footechCommented:
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
 
Guy LidbetterCommented:
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
 
footechCommented:
If there is a header, we could use Import-CSV.  Would just need to adjust lines 1 and 3.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now