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 Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

Leave a Reply

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

%d bloggers like this: