SharePoint Online: How to create a SharePoint Group through PowerShell!

This time I would like to share with you how to create a new SharePoint Group in a SharePoint Online site using PowerShell. You can download the script from the TechNet script gallery: How to create a SharePoint Group in a SharePoint Online Site

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

   2: #Script that allows to create a SharePoint Group in a SharePoint Online Site

   3: # Required Parameters:

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

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

   6: #  -> $sSiteCollectionUrl: SharePoint Online Site

   7: #  -> $sGroupToCreate: SPO Group to create

   8: #  -> $sGroupToCreateDescription: SPO Group description

   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 Create-SPOGroup

  15: {

  16:     param ($sSiteColUrl,$sUsername,$sPassword,$sGroupToCreate,$sGroupToCreateDescription)

  17:     try

  18:     {    

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

  20:         Write-Host "Creating SharePoint Group $sGroupToCreate in $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:         #Root Web Site

  33:         $spoRootWebSite = $spoCtx.Web

  34:         #Object for creating a new SPO Group

  35:         $spoGroupCreationInfo=New-Object Microsoft.SharePoint.Client.GroupCreationInformation

  36:         $spoGroupCreationInfo.Title=$sGroupToCreate

  37:         $spoGroupCreationInfo.Description=$sGroupToCreateDescription

  38:         $spoGroup=$spoRootWebSite.SiteGroups.Add($spoGroupCreationInfo)

  39:         $spoCtx.ExecuteQuery()

  40:         

  41:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  42:         Write-Host "SharePoint Group $sGroupToCreate in $sSiteColUrl created succesfully!!" -foregroundcolor Green

  43:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  44:         $spoCtx.Dispose()

  45:     }

  46:     catch [System.Exception]

  47:     {

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

  49:     }    

  50: }

  51:  

  52: #Required Parameters

  53: $sSiteColUrl = "https://<SPO_Site>/" 

  54: $sUsername = "<SPO_User>" 

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

  56: $sPassword=convertto-securestring "<Password>" -asplaintext -force

  57: $sGroupToCreate="CustomSPOGroup"

  58: $sGroupToCreateDescription="Custom SPO Group"

  59:  

  60: Create-SPOGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroupToCreate $sGroupToCreate -sGroupToCreateDescription $sGroupToCreateDescription

As you can see, create a SharePoint Group in a SharePoint Online is quite simple using the GroupCreationInformation object. And once you execute the script, this is the result you can see in your SharePoint Online site.

image_thumb

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 )

Google photo

Estás comentando usando tu cuenta de Google. 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 )

Conectando a %s