question

SathishKumarVenugopal-8529 avatar image
0 Votes"
SathishKumarVenugopal-8529 asked NewbieJones-6218 commented

Add List of users as Member to Azure AD group via Powershell

Hi All, I have a source.csv file with userID, UPN(UserPrinciplename), ObjectID, Email.

I'm fine with any of these attributes

I would like to add the list of users in my source.csv to a specific Azure AD group. The below example script is for Onprem that i get from a public forum which is really good. can someone help to find the same for Azure?

 # Start transcript
 Start-Transcript -Path C:\Temp\Add-ADUsers.log -Append
    
 # Import the data from CSV file and assign it to variable
 $Users = Import-Csv "C:\Script\Users.csv"
    
 # Specify target group where the users will be added to
 # You can add the distinguishedName of the group. For example: CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local
 $Group = "DLP20212022" 
    
 foreach ($User in $Users) {
     # Retrieve UPN
     $UPN = $User.UserPrincipalName
    
     # Retrieve UPN related SamAccountName
     $ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" | Select-Object SamAccountName
    
     # User from CSV not in AD
     if ($ADUser -eq $null) {
         Write-Host "$UPN does not exist in AD" -ForegroundColor Red
     }
     else {
         # Retrieve AD user group membership
         $ExistingGroups = Get-ADPrincipalGroupMembership $ADUser.SamAccountName | Select-Object Name
    
         # User already member of group
         if ($ExistingGroups.Name -eq $Group) {
             Write-Host "$UPN already exists in $Group" -ForeGroundColor Yellow
         }
         else {
             # Add user to group
             Add-ADGroupMember -Identity $Group -Members $ADUser.SamAccountName -WhatIf
             Write-Host "Added $UPN to $Group" -ForeGroundColor Green
         }
     }
 }
 Stop-Transcript
azure-active-directorywindows-server-powershellazure-ad-group-management
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SathishKumarVenugopal-8529 , just following up to check if the below script works for you . In case of any further queries , do let us know. If it helps , please do accept the post as answer so that it can help other members in the community with similar queries.

0 Votes 0 ·
ClementBETACORNE avatar image
1 Vote"
ClementBETACORNE answered EvgeniyStefanov-7418 commented

Hello,

Below an example of the script with the AzureAD cmdlet :

 Connect-AzureAD
    
 $Users = Import-Csv Users.csv -Delimiter ","
    
 $Group = "DLP20212022"
    
 foreach($user in $Users) {
     $AzureADUser = Get-AzureADUser -Filter "UserPrincipalName eq '$($user.UPN)'"
     if($AzureADUser -ne $null) {
         try {
             $AzureADGroup = Get-AzureADGroup -Filter "DisplayName eq '$Group'" -ErrorAction Stop
             $isUserMemberOfGroup = Get-AzureADGroupMember -ObjectId $AzureADGroup.ObjectId -All $true | Where-Object {$_.UserPrincipalName -like "*$($AzureADUser.UserPrincipalName)*"}
             if($isUserMemberOfGroup -eq $null) {
                 Add-AzureADGroupMember -ObjectId $AzureADGroup.ObjectId -RefObjectId $AzureADUser.ObjectId -ErrorAction Stop
             }
         }
         catch {
             Write-Output "Azure AD Group does not exist or insufficient right"
         }
     }
     else {
         Write-Output "User does not exist"
     }
 }

Note : My CSV file only have UPN column

Regards,




· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ClementBETACORNE Thank you for sharing your knowledge with the community .

0 Votes 0 ·

Hi ClementBETACORNE

Thanks for your replay, but i need to add owners, for many groups like 50thy. I want to add another csv with list with all groups.

Thank you!

0 Votes 0 ·
SafwanAlsalameh-3473 avatar image
0 Votes"
SafwanAlsalameh-3473 answered NewbieJones-6218 commented

Hi
What if I need to ad the user to many groups

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You should raise your own question. You don't resurrect a 2 year old already answered thread.

You should include what code you have already got.

Don't have any code? Need to learn PowerShell?

The following download is free.
https://www.sapien.com/books_training/Windows-PowerShell-4

Start with the first half of the book and do the exercises to cover the basics.
Then work through the 2nd half of the book which covers common tasks.

0 Votes 0 ·