Export termset into CSV using PnP PowerShell

In this article, I show you how you can Export termset into CSV using PnP PowerShell. Just for your information export term set functionality is not available using SharePoint central admin UI or SharePoint Site so we have to use PowerShell to export the term set.

 

SharePoint On-prem: Export termSet into CSV using PnP PowerShell

 

I am exporting the term set from SharePoint 2013 On-prem environment and this will work on SharePoint 2016 and SharePoint 2019 as well.

So let’s start following the below steps to export the taxonomy term set to CSV using PnP PowerShell.

Export term set to CSV using PnP PowerShell

The terms in the term set are nested and it will go up to 7 levels deep but for this article, I only extract the terms up to 3 levels if you need all levels kindly modify the script accordingly.

Below is the screenshot of how the term set looks in SharePoint –

 

Export termset using PnP PowerShell

 

Here the “Managed Metadata Service” top in the hierarchy above is called Term Store its ID is required in PowerShell script then the highlighted “University Group” is called a Term group inside that we have a term set and each term set contains nested terms.

Below is the script that exports the term set and its terms from a given term set into CSV using PnP PowerShell.

Note – Before running this script make sure PnP SharePoint 2013 module must be installed on your machine.

$StartDateTime = Get-Date
$ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$trmsetcsv = $ScriptPath + "\Exporttermset.txt" #output csv format
"Starting Export at: $($StartDateTime)" | Add-Content -Path $trmsetcsv
#Site URL
$SiteURL = "http://sp2013-sys.com"
#User Credentials change accordingly 
$userName = 'svc-frm'
$password = 'XXXXXX' | ConvertTo-SecureString -Force -AsPlainText
$cred = New-Object -typename System.Management.Automation.PSCredential($userName, $password)
[System.Collections.ArrayList] $termSetdata = [System.Collections.ArrayList]::new()
$termGroup = "University Group"
$termStoreGUID = "c9d6132v1be34f43729cc9ebb0akgd"
Write-Host "Please wait...Reading Taxonomy" -f Yellow
try{
Connect-PnPOnline -Url $SiteURL -Credentials $cred
$termSets = Get-PnPTermSet -TermGroup $termGroup -TermStore $termStoreGUID
$termSetdata = @()
foreach($termSet in $termSets) {
Write-host "Getting Terms from Term Set '$($termSet.Name)' from Term Group '$($termGroup)'" -f Green
"Getting Terms from Term Set '$($termSet.Name)' from Term Group '$($termGroup)'" | Add-Content -Path $trmsetcsv
$termStores = Get-PnPTerm -TermGroup $termGroup -TermSet $termSet.Name -termStore $termStoreGUID -IncludeChildTerms
$termSetdata += New-Object PsObject -Property @{
"Term Set Name" = $termSet.Name.Trim()
"Term Set Description" = $termSet.Description.Trim();
"LCID" = $termSet.ID;
"Available for Tagging" = $termSet.IsAvailableForTagging;
}
foreach ($termStore in $termStores) {
$termSetdata += New-Object PsObject -Property @{
"Term Set Name" = $termSet.Name.Trim();
"Term Set Description" = $termSet.Description.Trim();
"LCID" = $termSet.ID;
"Available for Tagging" = $termSet.IsAvailableForTagging;
"Level 1 Term" = $termStore.Name.Trim()
}
if($termStore.TermsCount -gt 0) {
foreach ($termStoreChild in $termStore.Terms) {
$termSetdata += New-Object PsObject -Property @{
"Term Set Name" = $termSet.Name.Trim();
"Term Set Description" = $termSet.Description.Trim();
"LCID" = $termSet.ID;
"Available for Tagging" = $termSet.IsAvailableForTagging;
"Level 1 Term" = $termStore.Name.Trim()
"Level 2 Term" = $termStoreChild.Name.Trim()
}
if ($termStoreChild.TermsCount -gt 0) {
foreach ($termStoreChild2 in $termStoreChild.Terms) {
$termSetdata += New-Object PsObject -Property @{
"Term Set Name" = $termSet.Name.Trim();
"Term Set Description" = $termSet.Description.Trim();
"LCID" = $termSet.ID;
"Available for Tagging" = $termSet.IsAvailableForTagging;
"Level 1 Term" = $termStore.Name.Trim()
"Level 2 Term" = $termStoreChild.Name.Trim()
"Level 3 Term" = $termStoreChild2.Name.Trim()
}
}
} 
}
}
}

$TermCollection = $termSetdata | Where-Object 'Term Set Name'.ToLower().Trim() -eq $termSet.Name.ToLower().Trim()
$TermCollection | Select-Object "Term Set Name", "Term Set Description", "LCID", "Available for Tagging", "Term Description", "Level 1 Term", "Level 2 Term", "Level 3 Term" | Export-Csv "./$($termSet.Name).csv" -NoTypeInformation -En UTF8
$TermCollection = $null

}
$termSetdata | Select-Object "Term Set Name", "Term Set Description", "LCID", "Available for Tagging", "Term Description", "Level 1 Term", "Level 2 Term", "Level 3 Term" | Export-Csv "./ExportTermSettoCSV.csv" -NoTypeInformation -En UTF8
Write-host "Taxonomy Data Exported into CSV" -f Yellow
$EndDateTime = Get-Date
"End Export at: $($EndDateTime)" | Add-Content -Path $trmsetcsv
}
catch{

$_.Exception.Source + ";"+ $_.Exception.Message +";" + $_.Exception.StackTrace | Add-Content -Path $trmsetcsv
}

 

 Output-

The below output contains all the terms set inside a “University Group” term group open the CSV and check the terms.

Sharepoint Export termset into csv

 

Conclusion

This is how you can export term set into CSV using PnP PowerShell in SharePoint On-prem environment and if you have the requirement to export term Set in SharePoint online have a look at this Export term set in SharePoint online using PnP PowerShell.