GitHub Icon Image
GitHub

Export Term Store terms to CSV

Summary

Script will export all or selected term groups' terms to CSV file.

Implementation

  • Open Windows PowerShell ISE
  • Edit Script and add details like SharePoint tenant URL, Term groups, and the output directory
  • Press run
  • CLI for Microsoft 365
  • PnP PowerShell
###### Declare and Initialize Variables ######  

#term store variables
$groups = @("Group 1","Group 2") # leave empty for exporting all groups

# data will be saved in same directory script was started from
$saveDir = (Resolve-path ".\")  
$currentTime= $(get-date).ToString("yyyyMMddHHmmss")  
$FilePath=".\TermStoreReport-"+$currentTime+".csv"  
Add-Content $FilePath "Term group name, Term group ID, Term set name, Term set ID, Term name, Term ID"

## Export List to CSV ##  
function ExportTerms
{  
    try  
    {  
        if($groups.Length -eq 0){ 
            $groups = @(m365 spo term group list | ConvertFrom-Json | ForEach-Object{ $_.Name })
        }
        # Loop through the term groups
        foreach ($termGroup in $groups) {
            try {
                $termGroupName = $termGroup
                Write-Host "Exporting terms from $termGroup"
                $query = "[?Name == '"+$termGroupName+"']"
                $termGroupObj = m365 spo term group list --query $query | ConvertFrom-Json 
                if($termGroupObj)
                {
                 $termSets = m365 spo term set list --termGroupName $termGroupName | ConvertFrom-Json 
                 foreach ($termSetObj in $termSets) {
                    $terms = m365 spo term list --termGroupName $termGroupName --termSetName $termSetObj.Name | ConvertFrom-Json 
                    foreach ($term in $terms) {
                        Add-Content $FilePath "$($termGroupObj.Name),$($termGroupObj.Id),$($termSetObj.Name),$($termSetObj.Id),$($term.Name),$($term.Id)"
                    }
                 }
               }
            }
            catch {
                Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
            }            
        }
     }  
     catch [Exception]  
     {  
        $ErrorMessage = $_.Exception.Message         
        Write-Host "Error: $ErrorMessage" -ForegroundColor Red          
     }  
}  
 
## Connect to SharePoint Online site  

$m365Status = m365 status
if ($m365Status -match "Logged Out") {
  Write-Host "Logging in the User!"
  m365 login --authType browser
}
 
## Call the Function  
ExportTerms
 
## Disconnect the context  
m365 logout  

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365


###### Declare and Initialize Variables ######  

#site url
$url="Site admin site url"

#term store variables
$groups = @("Group 1","Group 2") # leave empty for exporting all groups

# data will be saved in same directory script was started from
$saveDir = (Resolve-path ".\")  
$currentTime= $(get-date).ToString("yyyyMMddHHmmss")  
$FilePath=".\TermStoreReport-"+$currentTime+".csv"  
Add-Content $FilePath "Term group name, Term group ID, Term set name, Term set ID, Term name, Term ID"
## Export List to CSV ##  
function ExportTerms
{  
    try  
    {  
        if($groups.Length -eq 0){
            $groups = @(Get-PnPTermGroup | ForEach-Object{ $_.Name })
        }
        # Loop through the term groups
        foreach ($termGroup in $groups) {
            try {
                $termGroupName = $termGroup
                Write-Host "Exporting terms from $termGroup"
                $termGroupObj = Get-PnPTermGroup -Identity $termGroupName -Includes TermSets
                foreach ($termSet in $termGroupObj.TermSets) {
                    $termSetObj = Get-PnPTermSet -Identity $termSet.Id -TermGroup $termGroupName -Includes Terms
                    foreach ($term in $termSetObj.terms) {
                        Add-Content $FilePath "$($termGroupObj.Name),$($termGroupObj.Id),$($termSetObj.Name),$($termSetObj.Id),$($term.Name),$($term.Id)"
                    }
                }
            }
            catch {
                Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
            }
            
        }
     }  
     catch [Exception]  
     {  
        $ErrorMessage = $_.Exception.Message         
        Write-Host "Error: $ErrorMessage" -ForegroundColor Red          
     }  
}  
 
## Connect to SharePoint Online site  
Connect-PnPOnline -Url $Url -Interactive
 
## Call the Function  
ExportTerms
 
## Disconnect the context  
Disconnect-PnPOnline  
  

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?

Contributors

Author(s)
Ramin Ahmadi
Reshmee Auckloo

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI