Office 365 - Output Managed Metadata Term Sets and Terms using CSOM with PowerShell

This PowerShell script will connect to an O365 SharePoint tenant and output the following information to the console from the Managed Metadata Term Store:

  • Groups
  • Term Sets
  • Terms

As with my previous CSOM scripts, this is more a sample to get you started than something you would use in production. My term store has a relatively small amount of terms, I'm not sure how this script will behave with hundreds or thousands of terms!

Three variables need to be updated prior to running the script (highlighted), $User is the username of a tenant administrator, $TenantURL is the URL of the Tenant Admin Site and $Site is the URL of any Site within the tenant - this is simply used to bind to the Managed Metadata Service.

#Please install the SharePoint client components SDK - https://www.microsoft.com/en-us/download/details.aspx?id=35585 prior to running this script.

#Specify tenant admin and URL
$User = "admin@tenant.onmicrosoft.com"
$TenantURL = "https://tenant.admin.sharepoint.com"
$Site = https://.sharepoint.com/sites/site

#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

#Bind to MMS
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)
$Context.Load($MMS)
$Context.ExecuteQuery()

#Retrieve Term Stores
$TermStores = $MMS.TermStores
$Context.Load($TermStores)
$Context.ExecuteQuery()

#Bind to Term Store
$TermStore = $TermStores[0]
$Context.Load($TermStore)
$Context.ExecuteQuery()

#Retrieve Groups
$Groups = $TermStore.Groups
$Context.Load($Groups)
$Context.ExecuteQuery()

#Retrieve TermSets in each group
Foreach ($Group in $Groups)
    {
    $Context.Load($Group)
    $Context.ExecuteQuery()
    Write-Host "Group Name:" $Group.Name -ForegroundColor Green
    $TermSets = $Group.TermSets
    $Context.Load($TermSets)
    $Context.ExecuteQuery()
    Foreach ($TermSet in $TermSets)
        {
        Write-Host " Term Set Name:"$TermSet.Name -ForegroundColor Yellow
        Write-Host " Terms:" -ForegroundColor DarkCyan
        $Terms = $TermSet.Terms
        $Context.Load($Terms)
        $Context.ExecuteQuery()
        Foreach ($Term in $Terms)
            {
            Write-Host " " $Term.Name -ForegroundColor White
            }
        }
    }

Below is an example of the output from the script:

In my next post I plan to demonstrate how PowerShell can be used to create Term Sets and Terms using CSOM.

Brendan Griffin - @brendankarl

Comments

  • Anonymous
    January 01, 2003
    Nice article
  • Anonymous
    March 05, 2014
    In a follow up to my previous post - "Office 365 - Output Managed Metadata Term Sets and Terms using
  • Anonymous
    June 20, 2016
    This script works well however it doesn't appear to output any terms that are two or more levels deep in the term set structure. Would it be possible to modify the script to cater for these level 2+ terms?
    • Anonymous
      June 21, 2016
      Thanks, yes it should be possible to do this you'd need to bind to each term and the check for child terms. If I get chance i may Blog about this in the future.
  • Anonymous
    July 11, 2016
    HiThis is very useful . I have simplified your code so that I use the support of the PnPCommandlets i.e. remove the need to detect the MMS . One thing that would be useful to understand if a term has sub terms and if so list them nn levels deep.
  • Anonymous
    September 13, 2016
    Great article!How would I use this principle to update/overwrite a single existing termstore entry?Im asking as we have a termstore entry showing a value in our banner, we would like it to be updated on a daily basis with a number we get from an external source. Im hoping we could use powershell to connect to our O365 termstore and write and update the single termstore entry value. Is this possible and how would we go about doing that? :)
  • Anonymous
    May 09, 2017
    Excellent Article. Thanks a ton Brendan Griffin. I was facing the issue connecting to MMS. I just used you technique of Talent Admin URL it worked like charm for me.
  • Anonymous
    May 10, 2017
    The comment has been removed