Extract termstores in SharePoint 2013 using PowerShell


Have u created term store in SharePoint environment and want to extract it for business user review or trying to move the terms different environment. So let’s write PS script to extract the term stores. Make sure you create a folder named as Result and Log file inside the same folder where you are keeping the ps file

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


Read some SharePoint tutorials below:

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →

Leave a Reply