Author Posts

January 27, 2015 at 4:49 am

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 ?

January 27, 2015 at 4:56 am

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

Set-ADUser (Append to AD 'Notes' field)

Hope that helps!

January 27, 2015 at 4:59 am

$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 }
}

January 27, 2015 at 5:33 am

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