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 – prior to running this script.

#Specify tenant admin and URL
$User = ""
$TenantURL = ""
$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)

#Retrieve Term Stores
$TermStores = $MMS.TermStores

#Bind to Term Store
$TermStore = $TermStores[0]

#Retrieve Groups
$Groups = $TermStore.Groups

#Retrieve TermSets in each group
Foreach ($Group in $Groups)
    Write-Host "Group Name:" $Group.Name -ForegroundColor Green
    $TermSets = $Group.TermSets
    Foreach ($TermSet in $TermSets)
        Write-Host "      Term Set Name:"$TermSet.Name -ForegroundColor Yellow
        Write-Host "        Terms:" -ForegroundColor DarkCyan
        $Terms = $TermSet.Terms
        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 (7)

  1. Anonymous says:

    In a follow up to my previous post – "Office 365 – Output Managed Metadata Term Sets and Terms using

  2. Joe says:

    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?

    1. 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.

  3. DanTheManXX says:


    This 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.

  4. 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? 🙂

    1. One of my colleagues has Blogged about how to update the default term label, you could use this approach – Having said that for a value that us being updated daily I’m not sure that MMS is the best solution?