How to assign memberships to users in bulk by powershell?

Carol 96 Reputation points
2021-11-23T01:00:52.87+00:00

How to assign membership to users in bulk by powershell?
For example, I would like to assign a certain AD group to multiple users in bulk by using powershell.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Carol 96 Reputation points
    2021-12-13T01:48:52.307+00:00

    Since I can do this from online connection only, below is the method I could share with people.

    Install-Module AzureADPreview

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

    Connect-AzureAD

    $wytGroupName=Get-AzureADGroup -SearchString "input group name here"
    $List = Import-CSV ".\userlist.csv"
    ForEach ($User in $List)
    {

    $WyaUser=Get-AzureADUser -SearchString $User.UserPrincipalName
    Add-AzureADGroupMember -ObjectId $wytGroupName.ObjectId -RefObjectId $WyaUser.ObjectId
    

    }

    Disconnect-AzureAD

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Amit Singh 5,306 Reputation points
    2021-11-23T04:36:50.3+00:00

    You can checkout this helpful article - https://www.alitajran.com/add-users-to-group-powershell/


    Please mark as "Accept the answer" if the above steps helps you. Your suggestion will help others also !

    0 comments No comments

  2. Limitless Technology 39,926 Reputation points
    2021-11-26T08:33:15.26+00:00

    Hello ZhaoYinghua,

    Thank you for your question and reaching out.

    You can use below PowerShell script to add multiple users in to group.

    create .csv file with user list and run this script.

    Import active directory module for running AD cmdlets

    Import-module ActiveDirectory

    Store the data from UserList.csv in the $List variable

    $List = Import-CSV .\UserList.csv

    Loop through user in the CSV

    ForEach ($User in $List)
    {

    Add the user to the TestGroup1 group in AD

    Add-ADGroupMember -Identity TestGroup1 -Member $User.username
    }


    --If the reply is helpful, please Upvote and Accept as answer--


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.