Export SharePoint term set to CSV file with PnP PowerShell

To be able to import terms from a term set in one tenant to another tenant (for example if you have a test tenant and a production tenant), you need some way of exporting the term set from the source tenant. The import file should be in .csv format.

The PnP PowerShell Module has commands that works against the SharePoint Term Store. The functions we will use are Get-PnPTermGroup, Get-PnPTermSet and Get-PnPTerm where the last command fetches all terms recursively.

############################################################################
# Description: This script exports all terms from a term set to a CSV file.
############################################################################

param(
  [string]$siteUrl = "https://[tenant].sharepoint.com/sites/[sitename]",
  [string]$termGroupName = "[Term gruoup name]", # Example: "Company Taxonomy"
  [string]$termSetName = "[Term set name]", # Example: "Organisation Units"
  [string]$csvPath = ".\terms\[filename].csv"
)


# Connect to SharePoint Online
Connect-PnPOnline -Url $siteUrl -Interactive

# Retrieve the Term Set
$termGroup = Get-PnPTermGroup -Identity $termGroupName
$termSet = Get-PnPTermSet -Identity $termSetName -TermGroup $termGroup

# Retrieve all terms from the term set
$terms = Get-PnPTerm -TermSet $termSet -TermGroup $termGroup -Recursive

# Prepare the data for export
$exportData = @()
foreach ($term in $terms) {
    $exportData += New-Object PSObject -Property @{
        "TermId" = $term.Id
        "TermName" = $term.Name
        "TermNewName" = $term.Name # Use this column to store a new term name
    }
}

# Export the data to a CSV file
$exportData | Export-Csv -Path $csvPath -NoTypeInformation

Write-Host "Terms have been exported to $csvPath"

Leave a comment