Getting and changing site admins in SharePoint Online with PowerShell

In the SharePoint Online admin portal we can specify the primary admin and the list of other admins in the properties menu of the site.

Let’s look at how we can do it in PowerShell.

Prologue

To interact with SharePoint Online using PowerShell, we need Microsoft.Online.SharePoint.PowerShell module.

You can install in from the PowerShell Gallery using Install-Module cmdlet.

Install-Module Microsoft.Online.SharePoint.PowerShell

To update an already installed module to a new version, use the Update-Module cmdlet.

Update-Module Microsoft.Online.SharePoint.PowerShell

Need to say, that at the moment Microsoft.Online.SharePoint.PowerShell module works in Windows PowerShell only, it means, that you can’t use it in PowerShell Core 6.x.

To connect to SharePoint Online, you use Connect-SPOService cmdlet.

$credential = Get-Credential -Credential user@domain.com
Connect-SPOService -Url "https://YourTenant-admin.sharepoint.com" -Credential $credential

If you use multi-factor authentication – MFA, you can’t use -Credential parameter. Therefore, you command will look like this

Connect-SPOService -Url "https://YourTenant-admin.sharepoint.com"

and after you run this command, you will be prompted to enter username and password, as well as perform some additional actions to verify you identity.

To get the list of all existing sites, use the Get-SPOSite cmdlet.

Get-SPOSite

You can use this cmdlet’s results to copy specific site’s Url and paste it to the following cmdlets.

Primary admin

The Primary admin is the one who, unlike the other admins, receives e-mail notifications about various events, that occur in SharePoint, like when a site reaches its storage space limit.

To get the primary admin of some site, you can use the same cmdlet – Get-SPOSite. Site’s primary admin is specified in the Owner property of the returned Microsoft.Online.SharePoint.PowerShell.SPOSite object.

For example.

Get-SPOSite -Identity "https://YourTenant.sharepoint.com/sites/thesite"

To change site’s primary admin, use the Set-SPOSite cmdlet.

Set-SPOSite -Identity "https://YourTenant.sharepoint.com/sites/thesite" -Owner NewPrimaryAdmin@domain.com

Admins

Get site’s admins list

Concerning the list of other site admins, things are getting a little bit trickier.

To get admins list, we can use the Get-SPOUser cmdlet. We can determine, whether a user is a site admin, by checking the value of the IsSiteAdmin property.

Get-SPOUser -Site "https://YourTenant.sharepoint.com/sites/thesite" | Format-Table -Property DisplayName, LoginName, IsSiteAdmin

Need to say, that this list contains all admins, including the primary admin.

In order for the users, who are admins, to appear at the top of the command output, we can add the Sort-Object cmdlet to the pipeline.

Get-SPOUser -Site "https://YourTenant.sharepoint.com/sites/thesite" | Sort-Object -Property IsSiteAdmin -Descending | Format-Table -Property DisplayName, LoginName, IsSiteAdmin

And if you want the list to contain only the users, who are the site administrators, you can use the Where-Object cmdlet.

Get-SPOUser -Site "https://YourTenant.sharepoint.com/sites/thesite" | Where-Object -Property IsSiteAdmin | Format-Table -Property DisplayName, LoginName, IsSiteAdmin

Adding site admins

In order to specify user as the site admin, you use the Set-SPOUser cmdlet and its -IsSiteCollectionAdmin parameter.

Set-SPOUser -Site "https://YourTenant.sharepoint.com/sites/thesite" -LoginName NewAdmin@domain.com -IsSiteCollectionAdmin $true

Removing site admins

Similarly, to remove some user from admins list of the site, we specify $false as the value of the -IsSiteCollectionAdmin parameter.

Set-SPOUser -Site "https://YourTenant.sharepoint.com/sites/thesite" -LoginName NewAdmin@domain.com -IsSiteCollectionAdmin $false

Links:
GitHub: https://github.com/sethvs
Twitter: https://twitter.com/vsseth
Facebook: https://fb.com/inpowershell
VK: https://vk.com/inpowershell


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s