Ever had the need to migrate Terms from the Managed Metadata Term Store from one environment to another? Do you find the flat, CSV, import approach provided out of the box to be insufficient (especially with its lack of support for alternate labels)? When I first started working with Terms over two years ago I was extremely frustrated by the lack of export and import capabilities so I decided to solve the problem myself by creating two cmdlets, Export-SPTerms and Import-SPTerms. I’ve actually had these cmdlets publicly available for about two years now but I suspect very few people actually know they exist so I thought I’d put together this short post just to highlight them (I also recently pushed out an update which adds support for Site Collection scoped Groups if you have SP1 deployed).

I don’t want to go into a lot of detail regarding how these cmdlets work as they’re really very simple and the bulk of the code is just about iterating through the Term Store structure and turning the various objects into an XML structure (you can download the code from my downloads page). So with brevity in mind, here’s the full help for the Export-SPTerms cmdlet:

NAME
    Export-SPTerms
   
SYNOPSIS
    Export the Managed Metadata Terms.
   
SYNTAX
    Export-SPTerms [-TaxonomySession] <SPTaxonomySessionPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-TermStore] <SPTaxonomyTermStorePipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-Group] <SPTaxonomyGroupPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-TermSet] <SPTaxonomyTermSetPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-Term] <SPTaxonomyTermPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
   
DESCRIPTION
    Export the Managed Metadata Terms.
   
    Copyright 2011 Falchion Consulting, LLC
    > For more information on this cmdlet and others:
    >http://www.falchionconsulting.com/
    > Use of this cmdlet is at your own risk.
    > Gary Lapointe assumes no liability.
   

PARAMETERS
    -TaxonomySession <SPTaxonomySessionPipeBind>
        The TaxonomySession object containing the Term Stores to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -TermStore <SPTaxonomyTermStorePipeBind>
        The TermStore object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -Group <SPTaxonomyGroupPipeBind>
        The Group object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -TermSet <SPTaxonomyTermSetPipeBind>
        The TermSet object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -Term <SPTaxonomyTermPipeBind>
        The Term object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -OutputFile [<String>]
        The path to the file to save the terms to.
       
        Required?                    false
        Position?                    2
        Default value               
        Accept pipeline input?       false
        Accept wildcard characters?  false
       
    -AssignmentCollection [<SPAssignmentCollection>]
        Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.
       
        When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.
       
        Required?                    false
        Position?                    named
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".
   
INPUTS
   
   
   
OUTPUTS
   
   
   
NOTES
   
   
    For more information, type "Get-Help Export-SPTerms -detailed". For technical information, type "Get-Help Export-SPTerms -full".
   
    ——————EXAMPLE 1———————–
   
    PS C:\> Export-SPTerms -TaxonomySession "http://site/" -OutputFile "c:\terms.xml"
   
   
    This example exports the terms for all term stores associated with the site and saves to c:\terms.xml.
   
    ——————EXAMPLE 2———————–
   
    PS C:\> Export-SPTerms -Group (Get-SPTaxonomySession -Site "http://site/").TermStores[0].Groups[0] -OutputFile "c:\terms.xml"
   
   
    This example exports the first Group of the first Term Store and saves to c:\terms.xml.
   
   
RELATED LINKS
    Import-SPTerms
    Get-SPTaxonomySession

Now lets look at the Import-SPTerms cmdlet (it’s very similar):

NAME
    Import-SPTerms
   
SYNOPSIS
    Import the Managed Metadata Terms.
   
SYNTAX
    Import-SPTerms [-TaxonomySession] <SPTaxonomySessionPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTermStore] <SPTaxonomyTermStorePipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentGroup] <SPTaxonomyGroupPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTermSet] <SPTaxonomyTermSetPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTerm] <SPTaxonomyTermPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
   
DESCRIPTION
    Import the Managed Metadata Terms.
   
    Copyright 2011 Falchion Consulting, LLC
    > For more information on this cmdlet and others:
    > http://www.falchionconsulting.com/
    > Use of this cmdlet is at your own risk.
    > Gary Lapointe assumes no liability.
   

PARAMETERS
    -TaxonomySession <SPTaxonomySessionPipeBind>
        The TaxonomySession object to import Term Stores into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -ParentTermStore <SPTaxonomyTermStorePipeBind>
        The TermStore object to import Groups into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentGroup <SPTaxonomyGroupPipeBind>
        The Group object to import Term Sets into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentTermSet <SPTaxonomyTermSetPipeBind>
        The TermSet object to import Terms into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentTerm <SPTaxonomyTermPipeBind>
        The Term object to import Terms into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -InputFile <XmlDocumentPipeBind>
        The path to the file containing the terms to import or an XmlDocument object or XML string.
       
        Required?                    true
        Position?                    2
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -AssignmentCollection [<SPAssignmentCollection>]
        Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.
       
        When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.
       
        Required?                    false
        Position?                    named
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".
   
INPUTS
   
   
   
OUTPUTS
   
   
   
NOTES
   
   
    For more information, type "Get-Help Import-SPTerms -detailed". For technical information, type "Get-Help Import-SPTerms -full".
   
    ——————EXAMPLE 1———————–
   
    PS C:\> Import-SPTerms -ParentTermStore (Get-SPTaxonomySession -Site "http://site/").TermStores[0] -InputFile "c:\terms.xml"
   
   
    This example imports the Group from c:\terms.xml to the first Term Store.
   
    ——————EXAMPLE 2———————–
   
    PS C:\> Import-SPTerms -TaxonomySession "http://site/" -InputFile "c:\terms.xml"
   
   
    This example imports the terms from c:\terms.xml to the Term Store associated with http://site.
   
   
RELATED LINKS
    Export-SPTerms
    Get-SPTaxonomySession

Using these cmdlets I can easily export terms from one environment, such as production, to another environment, such as my test environment. This avoids the need to have backup and restore the database associated with the term store (which requires removing and re-provisioning the service application. And the way I’ve written the cmdlets all the Term Set, Group, and Term IDs remain consistent and, because the exported XML is easily modified, when it comes to Site Collection scoped Groups you can do a simple search and replace to change Site Collection URLs to match the new target (note that I first check for a Site Collection with the specified URL and if not found then I use the ID and if I still can’t locate the Site Collection then I create the Group as a standard shared Group).

-Gary