List AD groups use is a member of in notes field

Welcome Forums General PowerShell Q&A List AD groups use is a member of in notes field

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 years, 10 months ago.

  • Author
    Posts
  • #22174

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 18
    Team Member
    Rank: Member

    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!

  • #22176

    Member
    Points: 0
    Rank: Member

    $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

      Participant
      Points: 0
      Rank: Member

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

The topic ‘List AD groups use is a member of in notes field’ is closed to new replies.