Add an external user to multiple sites (via PowerShell)

April 29th, 2019 | Posted by Marijn in code | PnP Powershell | PowerShell

I have a customer that works a lot with external experts. Every project is a new SharePoint site, and contains multiple external experts. And, that is how life goes, these people tend to change email address from time to time. As a governance rule, we want these experts to stay in touch with their past projects. So every time someone changes email address, steps were made to add them to all the previous project sites. Manually…

When I got the request to add someone to 70 SharePoint sites, I immediatly thought: PowerShell!

I allready posted before how you can get all external users from all SharePoint sites in your tenant into an CSV (Excel) file. So starting from there, a better and easier solution had to be possible.

Step 1: Get a collection of all sites where you want to add this user

In my case, I didn’t know which 70 sites of our 800 site list I had to add this person. So I ran this script to list all external users from all sites in an CSV (excel) list. Then, I quickly could filter this list with the old email address of the user. Now I had a table of the correct sites.

To use them in PowerShell, I needed to have a collection of these url’s. From my programming days (waaaaaay back) I remembered there is something called an Array, which is a collection of items.

In PowerShell, an Array looks like this:

$array = “a”, “b”, “c”

This is a collection that has 3 items in it. So I needed to transform the items in Excel into a list like that. To do that, I used a simple Excel formula:

=CHAR(34) & item that has my url & CHAR(34) &”,”

This will get me (when I copy that into notepad) a list of all my sites, with double quotes and a comma. Delete the last comma from the last item.

Step 2: The script to add the user

First off, like any posts from me that include code… I am not a developer! This code can probably be improved 100 times over! But, as always.. it did the job for me!

So, now we can build our script, which is super easy. Just take the script below, and replace the “http://tenant.sharepoint.com/sites/” by the list of sites you just copied.

First , we ask the person who runs the script to enter the email address that we want to add. Next, we will have the collection of sites that we put in a collection, called “$ray”.

For each item in our collection, we connect to the site and add the person to a SharePoint group called “External”. (In our case the group exists for every site, change this however you want to) .

$ExternalEmailAddress = read-host -Prompt "Please enter the email address"

$ray = "https://tenant.sharepoint.com/sites/1 ",
"https://tenant.sharepoint.com/sites/2",
"https://tenant.sharepoint.com/sites/3",
"https://tenant.sharepoint.com/sites/4"

foreach ($element in $ray) {
     Connect-PnPOnline -Url $element -Credentials CRED
     Add-PnPUserToGroup -Identity External -EmailAddress $ExternalEmailAddress
     Write-Host "Person was added"
}

When you run the script, the person will be added to that group to all sites mentioned here. Ofcourse you could make the output much better so you have more audit possiblities.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

%d bloggers like this: