Welcome to a blog where we will find the duplicates in SharePoint metadata database, using PowerShell in the central admin.
Sometimes, we need to clean the metadata as the duplicate values are created when the multiple users update the metadata terms in central admin.
Here, I have a simple script for you to run with the minimal inputs from your end to identify the duplicates on your term store.
Let’s see how can we do it.
Steps
- Open Windows PowerShell Modules as an administrator.
- Paste the code, mentioned below as .ps1 file and execute it.
- #Add the PowerShell snap in code
- Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
- #Provide the site collection for the script to refer the Metadata database from the central admin
- $siteCollectionUrl = "http://devtest/"
- $web = "http://devtest/"
- #Read the metadata from SharePoint
- $site =new-object Microsoft.SharePoint.SPSite($siteCollectionUrl)
- $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
- $termStore = $session.TermStores[0]
- #Load the Term group
- $group = $termStore.Groups["Testgroup"]
- #Load the Term set
- $termSet = $group.TermSets["Testset"]
- $terms = $termSet.GetAllTerms()
- $Array = @()
- $newarray = @()
- $emailarray = @()
- foreach ($term in $terms)
- {
- $varval = $term.Name
- $lbl = $term.Labels[1].Value;
- $Array += $varval
- }
- #Check for duplicate Test set in SharePoint Metadata database
-
- $ht = @{}
- $Array | foreach {$ht["$_"] += 1}
- $ht.keys | where {$ht["$_"] -gt 1} | foreach {#write-host "Duplicate Sites found $_"
- $newarray += $_
- }
- foreach($obj in $newarray)
- {
- foreach ($term in $terms)
- {
- if ($term.Labels[1].Value -eq $obj)
- {
- $emailarray += $term.Name
- write-host $emailarray
- }
- }
- }
Pre-Requisites - Get the site collection URL.
- Get the Web.
- Get the term group name
- Get the term set, where you want to find the duplicates.
The script, mentioned above, the first gets the group “Test group” and the term “Testset”, where it reads the terms of “Testset” and assigns it to an array, where the array compares and finds the duplicates and results in the $emailarray as an output.
The script is so quick that it will hardly take any effort and time.