Extract term store in SharePoint 2013/2016 using PowerShell

This PowerShell SharePoint tutorial, we will discuss how to extract term store data in SharePoint 2013/2016 using PowerShell.

Have u created a term store in the SharePoint environment and want to extract it for business user review or trying to move the terms different environments. So let’s write a PowerShell script to extract the term store data.

Make sure you create a folder named as Result and Log file inside the same folder where you are keeping the ps file

PowerShell Script to extract term store in SharePoint

Below is the full PowerShell script which you can write, test and debug using PowerShell ISE.

if((Get-PSSnapin ‘Microsoft.SharePoint.PowerShell’ -ErrorAction SilentlyContinue) -eq $null){Add-PSSnapin ‘Microsoft.SharePoint.PowerShell’}
try{
#Create a variable based on the current date and time
$StartTime = (Get-Date -UFormat “%Y-%m-%d_%I-%M-%S %p”).tostring()

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$output = $(“$dp0\Result\TaxonomiesInformationReport_” +$StartTime +”.csv”)

$logFile=$(“$dp0\Logs\TaxonomiesInformationLog.txt”)
Write “Script started running at ” $StartTime >> $logFile

function loop{

param(

$Object,
$AttributeName,
$Level = 0
)

# check the child attribute containing the same type of objects
$Objects = iex “`$Object.$AttributeName”

# output this item
$Object | select @{L=”Object”;E={$_}}, @{L=”Level”;E={$Level}}

# output the child items of this object
if($Objects){

# add level
$Level ++

# loop trough the same function
$Objects | %{loop -Object $_ -AttributeName $AttributeName -Level $Level}
}
}

# reset vars
$SPTaxonomies = @()

# get all taxonomy objects
$SPTaxonomies = Get-SPTaxonomySession -Site “http://sp-stage-app02:2013/” | %{

$_.TermStores | %{

$TermStore = New-Object -TypeName Psobject -Property @{

TermStore = $_.Name
Group = “”
TermSet = “”
Terms = “”
}

$_.Groups | %{

$Group = $TermStore.PSObject.Copy()
$Group.Group = $_.Name

$_.TermSets | %{

$TermSet = $Group.PSObject.Copy()
$TermSet.TermSet = $_.Name
$TermSet.Terms = ($_.Terms | %{loop -Object $_ -AttributeName “Terms” -Level 1})

$TermSet
}
}
}
}

# get maximum of levels a term has
$Levels = ($SPTaxonomies | %{$_.Terms | %{$_.Level}} | measure -Maximum).Maximum + 1

# loop throught term stores
$SPTaxonomies | %{

$SPTaxonomy = $_

# loop throught terms
$_.Terms | %{

# create a term export object
$Item = $SPTaxonomy.PSObject.Copy()
$Index = 1;while($Index -ne $Levels){
$Item | Add-Member –MemberType NoteProperty –Name “Term Level $Index” –Value $(if($_.Level -eq $Index){$_.Object.Name}else{“”})
$Index += 1
}

# output this object
$Item | Select-Object * -exclude Terms
}
} | Export-CSV -path $output -notype
}
catch [Exception]{
Write $_.Exception|format-list -force >>$logFile
Write-Host -f red $_.Exception|format-list -force
}
$EndTime = (Get-Date -UFormat “%Y-%m-%d_%I-%M-%S %p”).tostring()
Write “Script stopped at” $EndTime >> $logFile

You may like following SharePoint PowerShell tutorials:

Here, we learned how to extract term store data in SharePoint 2013/2016 using PowerShell.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>