Office 365: How to add several users to SPO Sites using Add-SPOUser!

This time I’m sharing a PowerShell script that will allow you to add several users to different SharePoint Online (SPO) sites using Add-SPOUser cmdlet. All the required information (SPO Site Url, SPO Site Group and User Login) is read from a CSV file with the following structure:

Add_Users_To_SPO_File_Format

And this is the full script (you can download from here: https://gallery.technet.microsoft.com/How-to-add-serveral-users-a7e19ead)

############################################################################################################################################
#Script that allows to add Users to a SPO Group
# Required Parameters:
#  -> $sUserName: User Name to connect to the SharePoint Admin Center.
#  -> $sMessage: Message to show in the user credentials prompt.
#  -> sInputFile: CSV File with the Site Collections information.
############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that allows to add a user to a Group in a SharePoint Online Site
function Add-SPOUsersToGroup
{
    param ($sSiteColUrl,$sGroup,$sUserToAdd)
    try
    {    
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green
        Write-Host "Adding $sUserToAdd as member of $Group in $sSiteColUrl" -Foregroundcolor Green
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green
        
        Add-SPOUser -Site $sSiteColUrl -LoginName $sUserToAdd -Group $sGroup 
                
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green
        Write-Host "User $sUserToAdd succesfully added to $sSiteColUrl !!!" -Foregroundcolor Green
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green
    }
    catch [System.Exception]
    {
        Write-Host -Foregroundcolor Red $_.Exception.ToString()   
    }    
}

#Function that allows to add users to SharePoint Groups in different Site Collections.
#The information about the Site Collection, SharePoint Group and User to be added is read from a CSV file
function Add-SPOUsersToGroupFromCSV
{
    param ($sInputFile)
    try
    {   
        # Reading the Users CSV file
        $bFileExists = (Test-Path $sInputFile -PathType Leaf) 
        if ($bFileExists) { 
            "Loading $sInputFile for processing..." 
            $tblUsers = Import-CSV $sInputFile            
        } else { 
            Write-Host "$sInputFile file not found. Stopping the import process!" -foregroundcolor Red
            exit 
        }

    
        # Adding Users To Groups
        foreach ($spoUser in $tblUsers) 
        { 
          
            #$spoUser.SPOSCollection + $spoUser.SPOGroup 
            Add-SPOUsersToGroup -sSiteColUrl $spoUser.SPOSCollection -sGroup $spoUser.SPOGroup -sUserToAdd $spoUser.SPOUserLogin
        } 
    }
    catch [System.Exception]
    {
        Write-Host -Foregroundcolor Red $_.Exception.ToString()   
    } 
}
 
$sUserName = "<O365AdminUser>@<O365Domain>.onmicrosoft.com"
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  
$sPassword=ConvertTo-SecureString "<UserPassord>" -asplaintext -force
$sSPOAdminCenterUrl="https://<O365Domain>-admin.sharepoint.com/"
$msolcred = Get-Credential -UserName $sUserName -Message $sMessage
Connect-SPOService -Url $sSPOAdminCenterUrl -Credential $msolcred

$ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
$sInputFile=$ScriptDir+ "\<File_Name>.csv"

Add-SPOUsersToGroupFromCSV -sInputFile $sInputFile

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s