PowerShell Script to remove a Person from a People WebPart in a SharePoint online page

Nigel Foot 76 Reputation points
2022-09-27T13:18:44.397+00:00

Hi,

Can anyone tell me if is is possible to use a PowerShell Script to remove a Person from a People WebPart in a SharePoint online page?

I have the People WebPart and I have added people to this and its all working ok. I then want to remove a person when they leave the company but wondered if this can be scripted?

I was thinking that the script may be able to ask for the persons email address or user name and then look for that person in all of all of our sites webparts within the Tennent and remove them from these webparts?

Not sure if this is a possibility or not or even what the closest thing we could do to this ask?

TIA

Nigel

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,861 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
0 comments No comments
{count} votes

Accepted answer
  1. Limitless Technology 44,001 Reputation points
    2022-09-29T08:05:18.23+00:00

    Hello there,

    Parameters

    $TenantURL = "https:// xxxx.sharepoint. com"
    $UserID="i:0#.f|membership|user@X . com"

    Get Credentials to connect

    $Credential = Get-Credential

    Frame Tenant Admin URL from Tenant URL

    $TenantAdminURL = $TenantURL.Insert($TenantURL.IndexOf("."),"-admin")

    Connect to PnP Online

    Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential

    Get All Site collections - Filter BOT and MySite Host

    $Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL'"

    Iterate through all sites

    $Sites | ForEach-Object {
    Write-host "Searching in Site Collection:"$.URL -f Yellow
    #Connect to each site collection
    Connect-PnPOnline -Url $
    .URL -Credentials $Credential
    If((Get-PnPUser | Where {$.LoginName -eq $UserID}) -ne $NULL)
    {
    #Remove user from site collection
    Remove-PnPUser -Identity $UserID -Confirm:$false
    Write-host "`tRemoved the User from Site:"$
    .URL -f Green
    }
    }
    This PnP PowerShell removes the user from all SharePoint Online sites in the tenant.


    --If the reply is helpful, please Upvote and Accept it as an answer–


2 additional answers

Sort by: Most helpful
  1. Jinwei Li-MSFT 4,721 Reputation points Microsoft Vendor
    2022-09-28T08:34:28.44+00:00

    Hi @Nigel Foot ,

    Please try to use this:

    1.Input:

         $homePage = Get-PnPClientSidePage -Identity "page name"  
         $peoplePart = $homePage.Controls | ? {$_.Title -eq "People"}  
    

    245484-image.png

    2.Input:

    $peoplePart.PropertiesJson  
    

    245464-image.png

    This is all the users in your People web part.

    3.Remove the user you want.

    For example, delete the following highlighted areas.
    245409-image.png > 245416-image.png

    Input:

    $peoplePart.PropertiesJson = '{"persons":[{"id":"i:0#.f|membership|xxx@tenant.onmicrosoft.com","role":"Technical Support","firstName":"","lastName":"","upn":"","phone":"","sip":"","department":""}],"layout":1}'  
    

    4.Input:

         $homePage.Save()  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Dillon Silzer 54,751 Reputation points
    2022-09-27T16:00:56.027+00:00

    Hi @Nigel Foot

    I see a discussion about programmatically adding people using WebPart with Get-PnPClientSidePage at https://techcommunity.microsoft.com/t5/sharepoint-developer/adding-people-to-the-people-web-part-programmatically-using/m-p/110559

    Perhaps you could borrow the code to build a list and deploy it to the pages you specify (just an idea).

    You can also try using Get-PnPWebPartProperty, process the output, and save it back to SharePoint using Set-PnPWebPartProperty:

    Get-PnPWebPartProperty

    https://pnp.github.io/powershell/cmdlets/Get-PnPWebPartProperty.html

    Set-PnPWebPartProperty

    https://pnp.github.io/powershell/cmdlets/Set-PnPWebPartProperty.html

    -----------------------------

    If this is helpful please accept answer.

    0 comments No comments