Copy Group Memebership, Exclude Others

We have a current PS script for new AD account creation and in one section of the script we can model current memberships after an existing employee:


$PermissionModel = Read-Host "Enter the username to use as the model"

#try {
(Get-QADUser $PermissionModel).MemberOf | Add-QADGroupMember -Member $samaccountname
#catch {
# "An error occurred"

'Permissions granted same as ' + $PermissionModel
'Recording permissions for new user ..'


What I would like to add is...Copy all memberships EXCEPT groups that start with, or like "RMxxx". Trying to exclude some groups that require approval first before adding to group. How can I add to script to copy memberships, but exclude some?

  • Rick.J,

    Try this:

    $Groups = (Get-ADUser $PermissionModel -Properties MemberOf).MemberOf | ForEach-Object {
        (Get-ADGroup $_).Name
    } | Where {$_ -notlike "RM*"}
    $Groups | Add-ADGroupMember -Members $SamAccountName