Microsoft 365 : SharePoint Online – PowerShell script to add / remove given user as Site Collection Administrators

Microsoft 365 - Complete PowerShell script - to add user as a Site Collection Administrator
Microsoft 365 - Complete PowerShell script - to add user as a Site Collection Administrator

Hi All,

Greetings for the day !!!

Today sharing one more PowerShell script – script to update user permissions on SharePoint online site collection using PowerShell

Background / Use Case

  • In our tenant for certain site collections we need to perform some tasks / activity
  • To perform respective activity / tasks we need use our SharePoint admin account
  • So we need to give temporary permissions to our SharePoint admin account
  • Then perform the respective task / activity
    • Remove the permission again
    • So we are here using SharePoint Online PowerShell to give the permission to our account and remove again

      Takeaway

      • How to connect to SharePoint online using PowerShell
      • At the end of this article we got to know how to grant / revoke permissions to given user to site collection using PowerShell

      Prerequisites

      • Account at least SharePoint admin or Global admin role

      Details / Steps

      • Connect to SharePoint Online admin center – using Connect-SPOService

      #connect to SharePoint online admin center
      #User which executes these CMDLETs must be either SharePoint admin or Global admin
      Connect-SPOService -Url https://knowledgejunction1-admin.sharepoint.com/

      • Syntax

      Connect-SPOService
             -AuthenticationUrl <String>
             [-ClientTag <String>]
             [-Credential <CredentialCmdletPipeBind>]
             -Url <UrlCmdletPipeBind>
             -ModernAuth <Boolean>
             [<CommonParameters>]
             [-Region <AADCrossTenantAuthenticationLocation>]

      • Add the respective given user to respective site collection as site collection administrator using – Set-SPOUser
      • Here, we are adding user – KnowledgeJunction@knowledgejunction1.onmicrosoft.com to the site collection – https://knowledgejunction1.sharepoint.com/sites/Demo
      • IsSiteCollectionAdmin –
        • parameter determines the addition and removal of site collection administrator to the given site collection.
        • Specifies whether user is site collection administrator or not
        • If you want to remove the site collection administrator from the site collection, just set the value of this parameter to – FALSE

      #add the respective user as site collection administrator to the respective given site
      Set-SPOUser -Site https://knowledgejunction1.sharepoint.com/sites/Demo -LoginName KnowledgeJunction@knowledgejunction1.onmicrosoft.com -IsSiteCollectionAdmin $true

      • Syntax

      Set-SPOUser
         -IsSiteCollectionAdmin <Boolean>
         -LoginName <String>
         -Site <SpoSitePipeBind>
         [<CommonParameters>]

      fig : Microsoft 365 - Site collection - Site Collection Administrators - Only one site collection admin before executing the script
      fig : Microsoft 365 – Site collection – Site Collection Administrators – Only one site collection admin before executing the script

      Complete Script :

      try{
           #connect to SharePoint online admin center
           #User which executes these CMDLETs must be either SharePoint admin or Global admin
           Connect-SPOService -Url https://knowledgejunction1-admin.sharepoint.com/
      
           #add respective user as site collection administrator to the respective given site
           Set-SPOUser -Site https://knowledgejunction1.sharepoint.com/sites/Demo -LoginName 
           KnowledgeJunction@knowledgejunction1.onmicrosoft.com -IsSiteCollectionAdmin $true
        }#try
        catch{
              Write-Host "Exception occurred : $($_.Exception.Message)" -ForegroundColor Red
        }#catch

      Microsoft 365 - Complete PowerShell script - to add user as a Site Collection Administrator
      fiig : Microsoft 365 – Complete PowerShell script – to add user as a Site Collection Administrator
      fig : Microsoft 365 - Site collection - Site Collection Administrators - Knowledge Junction user added as site collection admin after executing the script
      fig : Microsoft 365 – Site collection – Site Collection Administrators – Knowledge Junction user added as site collection admin after executing the script

      Thanks for reading !!! HAVE A FANTASTIC TIME AHEAD 🙂

      Prasham Sabadra

      LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

      You may also like...

      Leave a Reply

      This site uses Akismet to reduce spam. Learn how your comment data is processed.