Bulk Add Terms, Term Sets, and Groups with PowerShell in SharePoint

Are you tired of manually managing your SharePoint term store? Do you find yourself repeatedly creating term groups, sets, and terms? If so, you're in luck! With PowerShell, you can automate these tasks, saving you time and effort.

In this blog post, we'll walk you through a PowerShell script that reads data from a CSV file and uses it to create or update term groups, sets, and terms in your SharePoint term store.

CSV Template

The CSV template should contain columns for "Term Group", "Term Set", and "Term" to define the structure of your SharePoint term store.

PnP PowerShell to Create Bulk Term Groups, Term Sets, and Terms from a CSV

$AdminCenterURL = "https://contoso-admin.sharepoint.com"
$CSVFile = "C:\Users\TermStoreData.csv"

Connect-PnPOnline -Url $AdminCenterURL -Interactive

#Get the Data from CSV
$CSVFile = Import-Csv $CSVFile

#Iterate through each row in the CSV file
ForEach ($Row in $CSVFile)
{
    # Get the term group
    $TermGroup = Get-PnPTermGroup -Identity $Row.TermGroup -ErrorAction SilentlyContinue
    if (!$TermGroup) {
        # Create the term group if it doesn't exist
        $TermGroup = New-PnPTermGroup -GroupName $Row.TermGroup
        Write-host "Term Group $($TermGroup.Name) Created Successfully!" -ForegroundColor Green
    }
    else
    {
        Write-host "Term Group $($TermGroup.Name) Exists Already!" -ForegroundColor Yellow
    }
    # Get the term set
    $TermSet = Get-PnPTermSet -Identity $Row.TermSet -TermGroup $TermGroup -ErrorAction SilentlyContinue
    if (!$TermSet) {
        # Create the term set if it doesn't exist
        $TermSet = New-PnPTermSet -Name $Row.TermSet -TermGroup $TermGroup -Lcid 1033
        Write-host "Term Set $($TermSet.Name) Created Successfully!" -ForegroundColor Green
    }
    else
    {
        Write-host "Term Set $($TermSet.Name) Exists Already!" -ForegroundColor Yellow
    }
    # Get the term
    $Term = Get-PnPTerm -Identity $Row.Term -TermSet $Termset -TermGroup $TermGroup -ErrorAction SilentlyContinue
    if (!$Term) {
        # Create the term if it doesn't exist
        $Term = New-PnPTerm -Name $Row.Term -TermSet $TermSet -TermGroup $TermGroup
        Write-host "Term $($Term.Name) Created Successfully!" -ForegroundColor Green
    }
    else
    {
        Write-host "Term $($Term.Name) Exists Already!" -ForegroundColor Yellow
    }
}

Understanding the Script

Let's break down the PowerShell script step by step.

Parameters: The script begins by defining two parameters: $AdminCenterURL and $CSVFile. These parameters specify the URL of your SharePoint admin center and the path to the CSV file containing your term store data.

Connect to Admin Center: The script connects to the SharePoint admin center using the Connect-PnPOnline cmdlet.

Read CSV Data: It reads the data from the CSV file using Import-Csv and calculates the total number of rows in the file.

Iterate Through Each Row: For each row in the CSV file, the script performs the following actions:

  • Check if the term group specified in the "TermGroup" column exists. If not, it creates a new one.
  • Check if the term set specified in the "TermSet" column exists within the term group. If not, it creates a new one.
  • Check if the term specified in the "Term" column exists in the term set. If not, it creates a new term.

Conclusion

Automating SharePoint term store management with PowerShell can streamline your workflow and ensure consistency across your organization's taxonomy. This script simplifies the process of importing term store data into SharePoint Online, ensuring the creation of term groups, term sets, and terms as specified in the CSV file.