Wednesday, September 21, 2011

Powershell script to print Managed Metadata termsets in a .csv file

Here is the powershell script to retrieve all Managed metadata term sets for a SharePoint 2010 site collection and print them into a .csv file

1. Copy the code below and modify the variables highlighted in yellow below, save the following as PrintManagedMetadataTerms.ps1 file:

Add-PsSnapin "Microsoft.SharePoint.PowerShell"

$site = Get-SPSite "http://dev-sp-2010:1000/sites/sharepointfix/"
$termStoreName = "Managed Metadata Service"
$termStoreGroupName = "SharePointFixPortal"

set-variable -option constant -name out -value "C:\PrintAllManagedMetaDataTermSets.csv"

$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[$termStoreName]
$termStoreGroup = $termStore.Groups[$termStoreGroupName]

# Prints all Managed Metadata Termsets in the .csv file at the configured location
foreach($termsets in $termStoreGroup.TermSets)
{
"Termset Name: " + $termsets.Name + ", Description: " + $termsets.Description + ", Group: " + $termsets.Group + ", ID: " +$termsets.Id + ", Available for Tagging: " + $termsets.IsAvailableForTagging + ", Is Open for Term Creation : " + $termsets.IsOpenForTermCreation | Out-File $out -append;

foreach($terms in $termsets.GetAllTerms())
{
"Term Name:" + $terms.Name | Out-File $out -append
}
}

$site.Dispose()

Echo "Finished!"
Echo "Managed metadata termsets printed at:" $out

Remove-PsSnapin "Microsoft.SharePoint.Powershell"

2. To automatically run the above .ps1 script as a batch utility, Copy and paste code below and save it with a .bat file extension

cd /d %~dp0
powershell -noexit -file ".\PrintManagedMetadataTerms.ps1" "%CD%"
pause

Run the above .bat file, on the receipt of success message, traverse to the configured path to find the .csv file with all the managed metadata printed values.