List AD groups use is a member of in notes field

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Johno Johno 1 year, 10 months ago.

  • Author
    Posts
  • #22174
    Profile photo of Johno
    Johno
    Participant

    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 ?

  • #22175
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

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

    Hope that helps!

  • #22176
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    $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 }
    }
    
    • #22178
      Profile photo of Johno
      Johno
      Participant

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

You must be logged in to reply to this topic.