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