I am running into issues with a script to list users and groups that each user is "member of" in a CSV file. I've tried numerous ways of exporting these objects and can't figure out how to remove the @{} tag in my output.
My output is:
@{Name=Jane Doe}
@{Group=Regional OU Admins}
Here is my code:
$OUPath = “OU=Computers, OU=HQ,OU=USA,DC=STACKOVERFLOW,DC=COM”
$users = (get-aduser -Filter * -SearchBase $OUpath | select SamAccountName)
ForEach ($User in $Users)
{ $Username = Get-ADUser $($user.SamAccountName) -Properties MemberOf
$Groups = ForEach ($Group in ($Username.MemberOf))
{ (Get-ADGroup $Group).Name
}
$Groups = $Groups | Sort
ForEach ($Group in $Groups)
{ New-Object PSObject -Property @{
Name = $Username.Name
Group = $Group
} | Select name,group | Add-Content C:\Temp\test.csv
}
}
What am I doing wrong here?