SharePoint Online Export termset into CSV using PnP PowerShell

In this article, I show you how you can export the termset using PnP PowerShell in SharePoint online. So let’s start following the below steps to export the taxonomy term set to CSV using PnP PowerShell.

If you want to Export the term set from a given term group in SharePoint on-prem kindly check this article.

 

SharePoint online Export termSet into CSV using PnP PowerShell

 

 

SharePoint Online Export termSet into 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.

Note – Make sure SharePoint Online Management Shell must be installed on your machine

 

$StartDateTime = Get-Date
$ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$termlogcsv = $ScriptPath + "\ExportMMSfromSPOnline_logs.txt"              #output csv format
"Starting Export at: $($StartDateTime)" | Add-Content -Path $termlogcsv

#Site URL
$tenantURL = "https://sponlinesite.sharepoint.com/"

#User Credentials change accordingly
$userName = 'your user name'
$password = 'your pwd' | 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"
Write-Host "Please wait..." -f Yellow
try{

Connect-PnPOnline $tenantURL -Credentials $cred
$termSets = Get-PnPTermSet -TermGroup $termGroup
$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 $termlogcsv

  $termStores = Get-PnPTerm -TermGroup $termGroup -TermSet $termSet.Name -IncludeChildTerms

  $termsetdata += New-Object PsObject -Property @{

              "Term Set Name" = $termSet.Name;

    "Term Set Description" = $termSet.Description;

              "LCID" = $termSet.ID;

              "Available for Tagging" = $termSet.IsAvailableForTagging;

  }

  foreach ($termStore in $termStores) {

    $termsetdata += New-Object PsObject -Property @{

              "Term Set Name" = $termSet.Name;

    "Term Set Description" = $termSet.Description;

              "LCID" = $termSet.ID;

              "Available for Tagging" = $termSet.IsAvailableForTagging;

    "Level 1 Term" = $termStore.Name;

 }

 if($termStore.TermsCount -gt 0) {

  foreach ($termStoreChild in $termStore.Terms) {

   $termsetdata += New-Object PsObject -Property @{

              "Term Set Name" = $termSet.Name;

    "Term Set Description" = $termSet.Description;

              "LCID" = $termSet.ID;

              "Available for Tagging" = $termSet.IsAvailableForTagging;

    "Level 1 Term"  = $termStore.Name;

    "Level 2 Term"  = $termStoreChild.Name;

   }

   if ($termStoreChild.TermsCount -gt 0) {

      foreach ($termStoreChild2 in $termStoreChild.Terms) {

                               $termsetdata += New-Object PsObject -Property @{

                                               "Term Set Name" = $termSet.Name;

                                                          "Term Set Description" = $termSet.Description;

                                                          "LCID" = $termSet.ID;

                                                          "Available for Tagging" = $termSet.IsAvailableForTagging;

                                                          "Level 1 Term"  = $termStore.Name;

                                                          "Level 2 Term"  = $termStoreChild.Name;

                                                          "Level 3 Term"  = $termStoreChild2.Name;

        }

                }

   }            

  }

 }

}




$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_$($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 $termlogcsv

}

catch{

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

}

Conclusion

This is how you can export term sets into CSV using PnP PowerShell in the SharePoint Online environment. Kindly change the input accordingly to your environment before running the script.