SharePoint Online: How to add a user to a SharePoint Group using PowerShell!

This time I would like to share a new PowerShell script ready to add users to SharePoint Groups in SharePoint Online using PowerShell. You can download the script from the TechNet Script Center Gallery: How to add users to a SharePoint Group in SharePoint Online

   1: ############################################################################################################################################

   2: #Script that allows to add Users to a SPO Group

   3: # Required Parameters:

   4: #  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

   5: #  -> $sPassword: Password for the user.

   6: #  -> $sSiteColUrl: SharePoint Online Site

   7: #  -> $sGroup: SPO Group where users are going to be added

   8: #  -> $sUserToAdd: User to be added

   9: ############################################################################################################################################

  10:  

  11: $host.Runspace.ThreadOptions = "ReuseThread"

  12:  

  13: #Definition of the function that allows to create a SharePoint Group in a SharePoint Online Site

  14: function Add-SPOUsersToGroup

  15: {

  16:     param ($sSiteColUrl,$sUsername,$sPassword,$sGroup,$sUserToAdd)

  17:     try

  18:     {    

  19:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  20:         Write-Host "Adding User $sUser to group $sGroup $sSiteColUrl" -foregroundcolor Green

  21:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  22:      

  23:         #Adding the Client OM Assemblies        

  24:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"

  25:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

  26:  

  27:         #SPO Client Object Model Context

  28:         $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) 

  29:         $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)  

  30:         $spoCtx.Credentials = $spoCredentials 

  31:         

  32:         #Getting the SharePoint Groups for the site                        

  33:         $spoGroups=$spoCtx.Web.SiteGroups

  34:         $spoCtx.Load($spoGroups)        

  35:         #Getting the specific SharePoint Group where we want to add the user

  36:         $spoGroup=$spoGroups.GetByName($sGroup);

  37:         $spoCtx.Load($spoGroup)       

  38:         #Ensuring the user we want to add exists

  39:         $spoUser = $spoCtx.Web.EnsureUser($sUserToAdd)

  40:         $spoCtx.Load($spoUser)

  41:         $spoUserToAdd=$spoGroup.Users.AddUser($spoUser)

  42:         $spoCtx.Load($spoUserToAdd)

  43:         $spoCtx.ExecuteQuery()     

  44:                 

  45:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  46:         Write-Host "SharePoint User $sUser added succesfully!!" -foregroundcolor Green

  47:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  48:         $spoCtx.Dispose()

  49:     }

  50:     catch [System.Exception]

  51:     {

  52:         write-host -f red $_.Exception.ToString()   

  53:     }    

  54: }

  55:  

  56: #Required Parameters

  57: $sSiteColUrl = "https://<Office365_Site>" 

  58: $sUsername = "<Office365_UserAccount>" 

  59: #$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

  60: $sPassword=convertto-securestring "<Office365_Password>" -asplaintext -force

  61: $sGroup="<SPO_Group>"

  62: $sUserToAdd="i:0#.f|membership|<Office365_UserAccount>"

  63:  

  64: Add-SPOUsersToGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroup $sGroup -sUserToAdd $sUserToAdd

As you can see, you have only to use the right CSOM objects so you can add a user to an existing SharePoint Group. And this is the result you get:

image_thumb[1]

SharePoint Online: Como añadir un usuario a un grupo por medio de PowerShell!

En esta ocasión os dejo un nuevo script que permite añadir usuarios a grupos de SharePoint en SharePoint Online por medio de PowerShell. Podéis descargaros el script desde el siguiente enlace: How to add users to a SharePoint Group in SharePoint Online

   1: ############################################################################################################################################

   2: #Script that allows to add Users to a SPO Group

   3: # Required Parameters:

   4: #  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

   5: #  -> $sPassword: Password for the user.

   6: #  -> $sSiteColUrl: SharePoint Online Site

   7: #  -> $sGroup: SPO Group where users are going to be added

   8: #  -> $sUserToAdd: User to be added

   9: ############################################################################################################################################

  10:  

  11: $host.Runspace.ThreadOptions = "ReuseThread"

  12:  

  13: #Definition of the function that allows to create a SharePoint Group in a SharePoint Online Site

  14: function Add-SPOUsersToGroup

  15: {

  16:     param ($sSiteColUrl,$sUsername,$sPassword,$sGroup,$sUserToAdd)

  17:     try

  18:     {    

  19:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  20:         Write-Host "Adding User $sUser to group $sGroup $sSiteColUrl" -foregroundcolor Green

  21:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  22:      

  23:         #Adding the Client OM Assemblies        

  24:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"

  25:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

  26:  

  27:         #SPO Client Object Model Context

  28:         $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) 

  29:         $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)  

  30:         $spoCtx.Credentials = $spoCredentials 

  31:         

  32:         #Getting the SharePoint Groups for the site                        

  33:         $spoGroups=$spoCtx.Web.SiteGroups

  34:         $spoCtx.Load($spoGroups)        

  35:         #Getting the specific SharePoint Group where we want to add the user

  36:         $spoGroup=$spoGroups.GetByName($sGroup);

  37:         $spoCtx.Load($spoGroup)       

  38:         #Ensuring the user we want to add exists

  39:         $spoUser = $spoCtx.Web.EnsureUser($sUserToAdd)

  40:         $spoCtx.Load($spoUser)

  41:         $spoUserToAdd=$spoGroup.Users.AddUser($spoUser)

  42:         $spoCtx.Load($spoUserToAdd)

  43:         $spoCtx.ExecuteQuery()     

  44:                 

  45:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  46:         Write-Host "SharePoint User $sUser added succesfully!!" -foregroundcolor Green

  47:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  48:         $spoCtx.Dispose()

  49:     }

  50:     catch [System.Exception]

  51:     {

  52:         write-host -f red $_.Exception.ToString()   

  53:     }    

  54: }

  55:  

  56: #Required Parameters

  57: $sSiteColUrl = "https://<Office365_Site>" 

  58: $sUsername = "<Office365_UserAccount>" 

  59: #$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

  60: $sPassword=convertto-securestring "<Office365_Password>" -asplaintext -force

  61: $sGroup="<SPO_Group>"

  62: $sUserToAdd="i:0#.f|membership|<Office365_UserAccount>"

  63:  

  64: Add-SPOUsersToGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroup $sGroup -sUserToAdd $sUserToAdd

Cómo veis, de nuevo basta con hacer uso de los objetos adecuados del modelo de objetos en cliente para poder añadir en este caso un usuario a un grupo de SharePoint existente. Y el resultado que se obtiene es el siguiente:

image