Microsoft 365 – PowerShell script – Adding users group – “Everyone except external users” to sites visitor group using PnP PowerShell CMDLET

M365 - PowerShell script - to add user group "Everyone except external user" to Site Visitors group
M365 - PowerShell script - to add user group "Everyone except external user" to Site Visitors group

Hi All,

Greetings for the day !!!

Today small but important PowerShell script

PowerShell script – PowerShell script to add users group – “Everyone except external users” in site visitors group using PnP PowerShell CMDLET

Background / Details

  • We have setup 50+ new SharePoint online communication sites (site collections – we maintained flat hierarchy) for one of our customer
  • We need to give read only permissions to every user on all sites
  • So we have Visitors group having read only permission for every site collection
  • So option is to add “Everyone except external users” group to site visitors group on all site collections
  • To perform above action we have written PowerShell script using PnP PowerShell script so sharing here , SHARING IS CARING 🙂

STEPS :

  • Preparing the CSV file
    • We will prepare CSV file having column – SiteUrls as
M365 - PowerShell script - CSV file having site urls
fig : M365 – PowerShell script – CSV file having site urls
  • Next to read the CSV file
    • We will use Import-Csv CMDLET as

Import-Csv "C:\Prasham\Articles\PowerShell\departmentsitelists.csv"

  • For each site in CSV file, we need to connect to the site
  • We are using Connect-PnPOnline CMDLET to connect to each site as

 Connect-PnPOnline -Url $_.SiteUrl #Here SiteUrl we are reading from CSV file

  • As we connected to the site, we will get associated visitor group using – Get-PnPGroup CMDLET

$visitorGroup = Get-PnPGroup -AssociatedVisitorGroup

  • Once we have associated visitor group, we will add user group “Everyone except external users” to the respective visitor group using – Add-PnPGroupMember CMDLET

Add-PnPGroupMember -LoginName 'everyone except external users' -Group $visitorGroup

Complete script

<#
   Reads site urls from CSV file and add group - "everyone except external users" 
   to site visitor group
   We are using PnP Online CMDLETS
   Writing success / failure logs to log file
   
   .NOTES
        Author : Prasham Sabadra
        Version: 1.0
#>

#importing CSV file - reading "SiteUrl" column
Import-Csv "C:\Prasham\Articles\PowerShell\departmentsitelists.csv" | foreach {

    try{
        #connecting to Site
            Connect-PnPOnline -Url $_.SiteUrl
        
        #getting Associated Visitors Group
            $visitorGroup = Get-PnPGroup -AssociatedVisitorGroup
        
  #Adding "everyone except external users" to respective associated visitors group
           Add-PnPGroupMember -LoginName 'everyone except external users' -Group 
           $visitorGroup
       
        #writing success message to log file
            $message = "everyone except external users - is successfully added to 
            the visitors group of site - " + $_.SiteUrl
            
           Write-Host $message
            Write-LogFile($message)

       }catch
       {
        #writing failure message to log file
            $message = "Failed to add - everyone except external users - visitors 
            group of site - " + $_.SiteUrl
            Write-Host $message
            Write-LogFile($message)
       } #catch
            
}#foeeach 


#log file path
$logFile = "C:\Prasham\Articles\PowerShell\logs.txt" 

#function to log the message in log file
Function Write-LogFile ([String]$Message)
{
    $outputmsg = [DateTime]::Now.ToUniversalTime().ToString("s") + ":" + $Message
    $outputmsg | Out-File $logFile -Append
}#Write-LogFile

M365 - PowerShell script - to add user group "Everyone except external user" to Site Visitors group
fig : M365 – PowerShell script – to add user group “Everyone except external user” to Site Visitors group

Thanks for reading!!! Please feel free to discuss in case any questions / suggestions / thoughts !!!

HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

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.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading