08 April 2021

Add guest users to AzureAD in bulk and update the manager with PowerShell

 Bulk invite guest users:

<#  
.NOTES
    Name      : Add-BulkGuests.ps1
    Author    : Edwin van Brenk
    Version   : 1.0
    Date      : 08-04-2021
    Requires  : PowerShell v2 or higher
                AzureAD module
.SYNOPSIS
    -
.DESCRIPTION
    Bulk invite guest users in your AzureAD tenant
.PARAMETER
    -
.EXAMPLE
    -
.CSV FILE
    The csv need to look like this:
    DisplayName,EmailAddress
    firstname lastname,firstname.lastname@domain.com

#>            
            
# Connect to your tenant            
Connect-AzureAD            
            
# Import the csv file            
$guests = Import-Csv C:\temp\BulkGuests2.csv            
            
# Invite all users in the imported csv file            
foreach ($guest in $guests)            
{            
# Function Variables            
$emailaddress = $guest.EmailAddress            
$displayname = $guest.DisplayName            
            
   New-AzureADMSInvitation -InvitedUserEmailAddress $guest.emailaddress -InvitedUserDisplayName $guest.displayName -InviteRedirectUrl https://myapplications.microsoft.com -SendInvitationMessage $True            
   Write-Host "Invite sent to $emailaddress" -ForegroundColor Green            
}            
            
Write-Host Finished

Update the managers for the newly invited guests:

<#  
.NOTES
    Name      : Update-GuestManager.ps1
    Author    : Edwin van Brenk
    Version   : 1.0
    Date      : 08-04-2021
    Requires  : PowerShell v2 or higher
                AzureAD module
                Az.Accounts
.SYNOPSIS
    -
.DESCRIPTION
    Update guest users in your AzureAD tenant with the correct manager
.PARAMETER
    -
.EXAMPLE
    -
.CSV FILE
    The csv needs to look like this:
    User,Manager
    firstname lastname,manageremailaddress@domain.com
    
#>            
            
            
# Connecting to AzureAD            
Connect-AzureAD            
Connect-AzAccount            
            
# Importing the CSV source which has the changes             
$data = Import-Csv C:\Temp\Bulk\Manager.csv            
            
# Iterating through each row in the CSV            
foreach ($row in $data)            
{            
# Find the user and the manager            
$user = Get-AzureADUser -SearchString $row.User | select objectid            
$manager = Get-AzADUser -UserPrincipalName $row.Manager | Select Id            
            
# Updating the manager             
Set-AzureADUserManager -ObjectId $user.objectid -RefObjectId $manager.id            
            
# Completion info in the console for the specified row            
Write-Host "Updated "$row.user"" -ForegroundColor Green            
            
# Clear the variable for the next row            
$user = $null            
$manager = $null            
            
}            
Write-Host "Finished" -ForegroundColor Green

No comments:

Post a Comment