Introduction
Recently the PnP module of SharePoint online which is SharePointPnPPowerShellOnline is no longer updated and considered as a legacy. MSFT is encouraging us to use PnP. Powershell module which is cross-platform which works on (Windows, macOS, Linux).
Pre-Requisites
In order to follow these steps, you need to have the latest version of the PnP PowerShell module installed. Please refer to the references section on installing and knowing more about PnP PowerShell module.
Operations using PnP PowerShell
Step 1
At first, we need to import the module.
Import-Module PnP.PowerShell
Step 2
Connect to SharePoint online site. Please note that this account needs to have SCA rights.
Connect-PnPOnline -Url "https://yourcompany.sharepoint.com/sites/sitename" -Interactive
Step 3
Store the current connection in variable $Connection
$Connection = Get-PnPConnection
Step 4
Now let's get the list of site collection administrators. To get a list of SCAs (site collection admins) run the following command. For getting this you do not need to specify a connection. This automatically considers the current context.
Get-PnPSiteCollectionAdmin
Step 5
Add the site collection admin. For adding, you do not need to specify a connection.
you can add multiple users by separating with a comma like below
Step 6
Remove site collection admin. For removing your need to query the user from the Get-PnPSiteCollectionAdmin command using where filter. Please note that I have to use Get-PnPSiteCollectionAdmin using the where filter. The command from the MSFT documentation is not working. It is throwing the error "unable to find the user".
Get-PnPSiteCollectionAdmin | where {$_.Email -eq "[email protected]"} | Remove-PnPSiteCollectionAdmin
You can remove all users from the site collection admin using the below command. But it is advised to keep at least one user or service account as site collection admin. You can also reach out to Global Admin or SharePoint admin if yourself accidentally removed your account and got locked out.
Get-PnPSiteCollectionAdmin | Remove-PnPSiteCollectionAdmin
Script Reference
- #Step1: Import PnP Powershell module
- Import-Module PnP.PowerShell
- #Step2: Connect to site collection. Please note that this account needs to have SCA. Replase url value to your needs
- Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/sitename" -Interactive
- #Step3: Get connection and store in a variable
- $Connection = Get-PnPConnection
- #Step4: to Get Site Collection Admin
- Get-PnPSiteCollectionAdmin
- #Step5: To Add site collection Admin for single users. Replace user upn values to your needs
- Add-PnPSiteCollectionAdmin -Owners @("[email protected]")
- #Step6: To Add site collection Admin for multiple users
- Add-PnPSiteCollectionAdmin -Owners @("[email protected]","[email protected]","[email protected]")
- #Step7: Removing the single user
- Get-PnPSiteCollectionAdmin | where {$_.Email -eq "[email protected]"} | Remove-PnPSiteCollectionAdmin
- #Removing multiple users
- Get-PnPSiteCollectionAdmin | Remove-PnPSiteCollectionAdmin
References
- https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps
- https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/?view=sharepoint-ps