3

How can I get a list of all active domain users with group membership and one user per line?

I have tried:

Import-Module Activedirectory
Get-ADUser -Filter 'enabled -eq $true' -Properties SamAccountname,DisplayName,memberof | % {
New-Object PSObject -Property @{
UserName = $_.DisplayName
Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join 
","}
} | Select SamAccountname,UserName,Groups 

but SamAccountname is empty.

2
  • 1
    That's a lot of pipes. Does the first command work as expected?
    – Dan
    May 18, 2017 at 13:31
  • yes, i get listing of username and groups, but SamAccountname field impty on all lines
    – Tester0
    May 18, 2017 at 13:54

2 Answers 2

3

You don't have SamAccountname because powershell searchs this property in your custom object which you create with New-Object. If you want to retrieve SamAccountname in this object you must modify to have :

Get-ADUser -Filter 'enabled -eq $true' -Properties SamAccountname,DisplayName,memberof | % {
New-Object PSObject -Property @{
UserName = $_.DisplayName
oSamAccountname= $_.SamAccountname
Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join 
","}
} | Select oSamAccountname,UserName,Groups 
0
2

This is fairly simple code, but it gave me a list of Users and their group memberships. I had to sort the results to get it looking as I wanted, but this does result in CSV file format, which makes it easier to manipulate in Excel.

$ADGroupList = Get-ADGroup -Filter * | Select Name -ExpandProperty Name | Sort Name
ForEach($Group in $ADGroupList)
{
  $members=Get-ADGroupMember -Identity $Group | Select Name, SAMAccountName | Sort
  ForEach($member in $members)
  {
  Write-Host ($member.Name + "," + $member.SAMAccountName  + "," + $Group.name)
  }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .