Deleting doublettes in SharePoint list with PnP PowerShell

In this blog post, I will break down a PnP PowerShell script that is designed to connect to a SharePoint site, retrieve a list of users from a specific SharePoint list (doublettes), identify duplicate users based on a unique identifier field, and then remove these duplicates. Of course you can use this script to remove any other kind of duplicate as well. Let’s dive into the details.
Step 1: Metadata Retrieval
Set the tenant name and site name for the SharePoint site. The tenant name is typically the domain of the organization, the site name is the specific site within the SharePoint environment that the script will interact with. The SharePoint list name is the name of the list within the site that contains the user data.
|
|
Step 2: Connecting to SharePoint
The script then connects to the SharePoint site using the PnP (Patterns and Practices) PnP PowerShell library, which is a set of PowerShell commands for SharePoint that allows for simpler scripting.
|
|
Step 3: Retrieving User List
Once connected, the script retrieves a list of all users from a specific list in SharePoint. It specifies the fields to retrieve and sets a page size of 5000, which is the maximum number of items that can be returned in a single request.
|
|
Step 4: Grouping and Sorting Users
The script then groups the users by ‘UniqueID’ and sorts them by the ‘Created’ date. It also filters out any users with a null ‘UniqueID’ or a ‘userPrincipalName’ that contains a specific domain.
|
|
Step 5: Identifying Duplicates
The script identifies duplicate users (users with the same ‘UniqueID’) and stores them in an array. It skips the first user in each group of doublettes, meaning it will keep the oldest user (based on the ‘Created’ date) and mark the rest as duplicates.
|
|
Step 6: Deleting Duplicates
Finally, the script loops through the array of duplicate users and deletes them from the specific list in batches. This is done using the ‘Remove-PnPListItem’ command, which removes a list item from a specified list.
|
|
And that’s it! This script provides a handy way to manage users in a SharePoint site, specifically by removing duplicate users based on their ‘UniqueID’. It’s a great example of how PowerShell with the help of PnP PowerShell can be used to automate administrative tasks in SharePoint.
Full Script for Reference
Here’s the full script for your reference:
|
|
Published on:
Learn moreRelated posts
Silently Update SharePoint Metadata with PnP PowerShell
Why Bother with Metadata? Think of metadata as the DNA of your SharePoint content. It tells you who created a document, when it was last touch...
New Editing Options for Image Web Part in SharePoint Online
Adding and Editing Images with the Image Web Part The Image web part simplifies the process of adding visual elements to your SharePoint Onlin...
External User Access Reviews in Office 365
Understanding External User Access Before diving into the reviews, it’s important to understand what external access entails. External u...
Power Automate - How-to Posting on BlueSky and Mastodon
Introduction Do you want to save time and effort by automating your social media posts across different platforms? Do you want to learn how to...
M365 Groups: Set Up a 'No Owner Policy' & why it's Important
Introduction Managing group ownership is crucial for maintaining order and security within an organization in Microsoft 365. A “No Owner...
Social media content creator with AI Promts
Prerequisites For this tutorial you need the following: A premium Power Automate account, e.g. a Power Automate per user plan or an Powerapps...
Change SharePoint group to Security Groups with PowerShell
Automating SharePoint Permissions with PowerShell Managing SharePoint user permissions can be a complex and time-consuming task, especially fo...
Limit Copilot's access to SharePoint Sites and Content
Why Permissions in Office365 matter for Copilot In the ever changing digital landscape, managing permissions and ensuring data security are pa...
Microsoft Teams Channel Types: How to Choose the Right One
Introduction Microsoft Teams is a powerful collaboration tool that allows you to communicate, share, and work with your team members in a secu...
Getting Started with Power Platform dev environment
Power Platform is a suite of low-code/no-code tools that enable you to build PowerApps, Power Automate workflows, and analyze data PowerBI. Th...