Try the following. This will be one line per group with displayName and whenCreated attributes for the user repeated for each group.
Please get into the habit of only bringing back the properties you need. (Instead of just using *, which will be every property for every user, which affects the performance of your script).
$Users = Get-ADuser -Filter * -properties memberof, whenCreated, DisplayName
$results=@()
ForEach ($User in $Users) {
$groups = $User.memberOf | Get-ADGroup | Where-Object {$_.GroupCategory -eq 'Security'}
ForEach ($group in $groups) {
$props = [ordered]@{
displayName=$user.displayName
whenCreated=$user.whenCreated
Group=$group.Name
GroupCategory=$group.GroupCategory}
$results += New-Object -TypeName PSObject -property $props
}
}
$results | Export-CSV filename.csv -noTypeInformation
Attached the screenshot for reference. GroupName is not getting populated. And also would like this to export as either HTML or CSV.