Working With PnP PowerShell

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.
 
Add-PnPSiteCollectionAdmin -Owners @("user@contoso.onmicrosoft.com") 
 
you can add multiple users by separating with a comma like below 
 
Add-PnPSiteCollectionAdmin -Owners @("user1@contoso.onmicrosoft.com", "user2@contoso.onmicrosoft.com")
 
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 "user@contoso.onmicrosoft.com"} | 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
  1. #Step1: Import PnP Powershell module    
  2. Import-Module PnP.PowerShell     
  3. #Step2: Connect to site collection. Please note that this account needs to have SCA. Replase url value to your needs    
  4. Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/sitename" -Interactive    
  5. #Step3: Get connection and store in a variable    
  6. $Connection = Get-PnPConnection    
  7. #Step4: to Get Site Collection Admin    
  8. Get-PnPSiteCollectionAdmin    
  9. #Step5: To Add site collection Admin for single users. Replace user upn values to your needs    
  10. Add-PnPSiteCollectionAdmin -Owners @("user1@contoso.onmicrosoft.com")    
  11. #Step6: To Add site collection Admin for multiple users    
  12. Add-PnPSiteCollectionAdmin -Owners @("user1@contoso.onmicrosoft.com","user2@contoso.onmicrosoft.com","user3@contoso.onmicrosoft.com")    
  13. #Step7: Removing the single user    
  14. Get-PnPSiteCollectionAdmin | where {$_.Email -eq "user1@contoso.onmicrosoft.com"} | Remove-PnPSiteCollectionAdmin    
  15. #Removing multiple users    
  16. 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