Sharepoint Online and Powershell Script to Remove a user from webparts on different Sites.

Nigel Foot 76 Reputation points
2022-09-29T23:42:53.343+00:00

I have this put together this script, which will ask for the TargetSiteUrl and the HomePageName of the page containing the "People" WebParts that have been inserted into some of our sharepoint pages. I want to remove a user from multiple pages in different sites. However this script will only do one Site/Page and I have to remove the user manually by taking them out of the "$peoplePart.PropertiesJson" section of the code and then running the script to remove them from the webpart in the pages that I specify when the script runs.

I would like the script to be able to ask for the UserID (first.last@xxxxxxxxxxxxx .com) that I want to remove from each of the hard coded sites/pages that I put into the script. I want the script to search the hard coded sites/pages and remove the specified UserID (first.last@xxxxxxxxxxxxx .com) from the people WebParts included in each page.

TIA

Nigel

`Do{

Config Parameters

$Tenanturl = "https://xxxxxxx.sharepoint.com"
$TargetSiteUrl = Read-Host "Enter the Destination Site Url (EXAMPLE: sites/IT)"
$HomePageName = Read-Host "Please enter the name of the page containing the People WebPart"

Write-Host "The entered Tenant & Site Url is:" $Tenanturl/$TargetSiteurl -ForegroundColor Green
Write-Host "The entered HomePage Name is:" $HomePageName -ForegroundColor Green

$Confirmation = Read-Host -Prompt "Is this the correct Site information and HomePageName? (y/n)"
}

while ($Confirmation -ne "y")

Connect-PnPOnline -Url "$tenanturl/$TargetSiteUrl" -Interactive
Write-Host "Connected - Please wait while information you entered is assessed....." -ForegroundColor Green

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

$peoplePart.PropertiesJson = '{"persons":[{"id":"first.last@Piepel .com","role":"PlaceRoleHere","department":"PlaceDepartmentHere"},{"id":"first.last@Piepel .com","role":"PlaceRoleHere","department":"PlaceDepartmentHere"}],"layout":1}'

$homePage.Save()
Write-Host "Your changes have been saved to the page now" -ForegroundColor Green
pause
`

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,607 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,361 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,116 Reputation points
    2022-09-30T06:56:49.217+00:00

    Hi @Nigel Foot ,

    According to my research and testing, unfortunately, there is currently no PowerShell scripts to remove users from Web Parts on different sites. As you said, you need to manually remove user from $peoplePart.PropertiesJson.

    Also, if you want to remove a user from all SharePoint Online sites, you can use the following script:

    #Parameters  
    $TenantURL =  "https://crescent.sharepoint.com"  
    $UserID="i:0#.f|membership|sharaz@crescent.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  
        }  
    }  
    

    More information for reference: SharePoint Online: PowerShell to Remove User from All Sites

    Hope it can help you. Thanks for your understanding.

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.