A Guide to Manage SharePoint Online Users and Groups with PowerShell

A Guide to Manage SharePoint Online Users and Groups with PowerShell

SharePoint Online is a popular collaboration platform used by organizations to store, manage and share files, folders, and other resources. Although SharePoint has various built-in functions to manage users and groups, these settings may not be sufficient for all needs. Fortunately, PowerShell provides a powerful and flexible way to SharePoint Online reporting.

To manage SharePoint Online users and groups with PowerShell, admins must first install and connect to SharePoint Online PowerShell module using either the global administrator or SharePoint administrator account. In this blog, we will guide you through the steps to manage SharePoint Online users and groups using PowerShell effectively! 

 

Get All SharePoint Sites Using PowerShell

You can get the list of SharePoint Online sites present in your tenant using the following cmdlet. 

Get-SPOSite 

Get list of sites using PowerShell

You can also find and export all the SharePoint Online site collections and their required details using PowerShell.

 

Create a Group in SharePoint Online Using PowerShell

To create and add a new SharePoint Online group to a site, use the ‘New-SPOSiteGroup cmdlet. 

New-SPOSiteGroup -Group <NewGroupName> -PermissionLevels <Level> -Site <SharePointSiteURL> 

Get list of sites using PowerShell

 

Update Group Properties in SharePoint Online Using PowerShell 

The properties of an existing SharePoint Online group can be updated using the ‘Set-SPOSiteGroup‘ cmdlet.  

For example, 

Set-SPOSiteGroup -Identity <GroupName> -Site <SharePonitSiteURL> -PermissionLevelsToAdd <Level> 

In this example, the command adds another permission level to the existing group. Update Group Properties in SharePoint Online with PowerShell

 

Add a User to SharePoint Group Using PowerShell 

To add a user to a SharePoint Online group on a site collection, you can use the ’Add-SPOUser‘ cmdlet. 

Add-SPOUser -Group <GroupName> -LoginName <UPN> -Site <SharePointSiteURL> 

Add a User to SharePoint Group Uing PowerShell

To add multiple users to a SharePoint Online group efficiently, refer the PowerShell script to add bulk users to SharePoint Online Group.

 

Add User to Site Collection Administrators Group 

To add a user to the site collection administrator group using PowerShell, you can utilize the ‘Set-SPOUser cmdlet.

Set-SPOUser -Site <SharePointSiteURL> -LoginName <UPN> -IsSiteCollectionAdmin $true 

Add a User to Site Collection Administrators Group Using PowerShell

 

Remove a User from a SharePoint Group

The ‘Remove-SPOUser‘ cmdlet can be used to remove a single Microsoft 365 user from a SharePoint Online site collection group.

Remove-SPOUser -LoginName <UPN> -Site <SharePointSiteURL> -Group <GroupName> 

You can remove a user from all groups they belong to by running the following cmdlet.

Get-SPOSite |ForEach {Remove-SPOUser -LoginName <UPN> -Site $_.Url}

And also, to bulk remove any unnecessary users from the groups,refer the PowerShell script to remove bulk users from SharePoint Online group.

 

Get SharePoint Online Group and Members with PowerShell

To list all the groups available in the specific site, use the ‘Get-SPOSiteGroup’ cmdlet. 

Get-SPOSiteGroup –Site <SiteURL>

To view SharePoint Online group members, you can run the ‘Get-SPOUser’ cmdlet with site URL and the group name.

Get-SPOUser –Site <SiteURL> -Group <GroupName>

You can also refer our blog to view all the groups in SharePoint Online and to view the SharePoint Online group membership report.

 

Export SharePoint Online Users Using PowerShell

Using the below cmdlet, you can export the SharePoint Online user details for a specific site to a TXT or CSV file. 

Get-SPOUser -Site <SharePointSiteURL> | Out-File <FilePath> -Append

Note:  The Out-File cmdlet is used to write the output to a file. The -Append parameter appends the output to the file specified in the <FilePath> parameter, rather than overwriting it. 

You can also export the SharePoint Online user details for all SharePoint sites to a TXT or CSV file by using the below cmdlet.

Get-SPOSite | ForEach {Get-SPOUser –Site $_.Url}| Out-File <FilePath> -Append

 

We hope this article has provided an overall idea about how to manage SharePoint Online users and groups using PowerShell. If you have any questions or comments, feel free to share them in the comment section below. 

A Guide to Manage SharePoint Online Users and Groups with PowerShell

by Thiraviam time to read: 3 min
0