List AD groups use is a member of in notes field

Hi all,
I’m trying to make a power shell script that will ask for a user name & then insert a list of the AD groups the specified user is a member of into the notes field in the telephone tab for the specified user.
I keep getting an empty notes field & this is what I’ve got so far .

function copygroups {
$UserID = Read-Host “username : "
$Groups= $UserID.MemberOf -join “`r`n”
Set-ADUser $UserID -Replace @{Info=”$($Groups)`r`n"}
}

I’ve also been trying

function copygroups {
$UserID = Read-Host “username : "
$Groups = Get-ADPrincipalGroupMembership $userid | select name
Set-ADUser $userid -replace @{Info=”$($Groups)`r`n"}
}

Is anyone able to advise where I’m going wrong & how I might be able to get this to work ?

Hey there Johno. You might want to look into this. Another user here was looking at a similar challenge.

https://powershell.org/forums/topic/set-aduser-append-to-ad-notes-field/

Hope that helps!

$UserID is just a string and has no MemberOf property, in the first one (though you could fetch the user and get that property from Get-ADUser). MemberOf contains distinguished names, though, which might get a bit wordy in your notes attribute.

Your second version looks closer to working, and would . Maybe try this:

function copygroups {
    param (
        [Parameter(Mandatory = $true)]
        [string] $UserID
    )

    $Groups = (Get-ADPrincipalGroupMembership $UserID -ErrorAction Stop | Select-Object -ExpandProperty name) -join "`r`n"
    Set-ADUser $UserID -Replace @{ Info = $Groups }
}

Thanks Dave, that’s exactly what i was trying to achieve