Sunday, May 26, 2013

Export Term Store to csv using PowerShell

This is another approach towards exporting the term stores to a CSV file format using power shell. I have tried to modify and enhance my previous post to achieve this requirement of generating a  Microsoft Excel format.

So here it is.

#creating a header-row for the csv file

"termStore `t" + "termGroup `t" + "termGroup-LastModifiedDate `t" + "termSet `t" + "termSet-LastModifiedDate `t" + "termSetGUID `t" + "terms `t" + "termsGUID `t" + "terms-LastModifiedDate" >> C:\ExportTermStore.csv

$termSet = Get-SPTaxonomySession -Site http://mydomain/sites/cthub
$termStore = $termSet.TermStores[0]
$termStore.Groups | FT -Autosize Name
$termGroup = $termStore.Groups["myTermStoreGroupName"]   #This is the case when we know the name of the group and want just that. We can also have a foreach instead, to loop-through each of the TermStore Group. I chose the first.
$termStore = $termStore.Name #saving the termStore “Name” in the variable $termStore
$termGroup.TermSets | FT -Autosize Name

foreach ($termGroups in $termGroup.TermSets)
{
  foreach ($termSets in $termGroups.Terms)
  {
    foreach ($terms in $termSets.Terms)
    {
      $termStore + "`t" + $termGroups.Name + "`t" + $termGroups.LastModifiedDate + "`t" + $termSets.Name + "`t" + $termSets.LastModifiedDate + "`t" + $termSets.ID + "`t" + $terms.Name + "`t" + $terms.ID + "`t" + $terms.LastModifiedDate >> C:\ExportTermStore.csv
    }
  }
}

No comments: