#######################################
# Create a SharePoint Group in a SharePoint Online Site using PowerShell O365
# Required Parameters:
# Author: Gowtham Rajamanickam
# Date:12-March-2017
########################################
- # create a SharePoint Group in a SharePoint Online Site
- function Create-Group
- {
- param ($SPonlineUrl,$Username,$Password,$GroupName,$GroupDescription)
- try
- {
- #Adding the Client OM Assemblies
- Add-Type -Path "C:\CSOMDLL\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\CSOMDLL\Microsoft.SharePoint.Client.Runtime.dll"
- #create context for connect with SharePoint online
- $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SPonlineUrl)
- $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
- $Ctx.Credentials = $Credentials
- $RootWebSite = $Ctx.Web
- $GroupCreationInfo=New-Object Microsoft.SharePoint.Client.GroupCreationInformation
- $GroupCreationInfo.Title=$GroupToCreate
- $GroupCreationInfo.Description=$GroupToCreateDescription
- $Group=$spoRootWebSite.SiteGroups.Add($GroupCreationInfo)
- $Ctx.ExecuteQuery()
- $Ctx.Dispose()
- }
- catch [System.Exception]
- {
- write-host -f red $_.Exception.ToString()
- }
- }
- #Enter the below manatory Parameters
- $SPonlineUrl = "https://gowthamr.sharepoint.com/"
- $Username = "*******"
- $Password=convertto-securestring "*****" -asplaintext -force
- $GroupName="Gowtham_Group"
- $GroupDescription="CustomGroup"
- Create-Group -sSiteColUrl $SPonlineUrl -sUsername $Username -sPassword $Password -sGroupToCreate $GroupName -sGroupToCreateDescription $GroupDescription
Run the code given above and check your SharePoint Site. A new group is created successfully.
Was my blog helpful?
If my blog is helpful, please let us know at the bottom of this page. If it is not helpful, let us know what was confusing or missing. I’ll use your feedback to double-check the facts, add info and update this blog.
If my blog is helpful, please let us know at the bottom of this page. If it is not helpful, let us know what was confusing or missing. I’ll use your feedback to double-check the facts, add info and update this blog.

henry jonesPosted Aug 3, 2018, 1:48 PM
Here is error message I am receiving: PS C:\scripts> .\NewGroupsExtLearningStoredCredAt C:\scripts\NewGroupsExtLearningStoredCred.ps1:38 char:38 + $GroupDescription="Blue Ridge Members" + ~ The string is missing the terminator: ". At C:\scripts\NewGroupsExtLearningStoredCred.ps1:6 char:5 + { + ~ Missing closing '}' in statement block or type definition. At C:\scripts\NewGroupsExtLearningStoredCred.ps1:40 char:155 + ... GroupToCreate $GroupName -sGroupToCreateDescription $GroupDescription + ~ The Try statement is missing its Catch or Finally block. At C:\scripts\NewGroupsExtLearningStoredCred.ps1:3 char:1 + { + ~ Missing closing '}' in statement block or type definition. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString PS C:\scripts>
henry jonesPosted Aug 3, 2018, 1:48 PM
I am getting the following error message with script above: