SharePoint Online: Get All Terms from a Term Set using PowerShell
Requirement: PowerShell to Get All Terms from a Term Set in SharePoint Online
SharePoint Online: Get Terms from a Term Set using PowerShell
PowerShell provides a scriptable way to manage SharePoint Online. In this blog post, let’s look at how you can use PowerShell to get terms from a term set in SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
#Function to recursively get all child terms from a Term
Function Get-SPOTerm([Microsoft.SharePoint.Client.Taxonomy.Term]$Term)
{
Write-host $Term.Name
#Get All child terms
$ChildTerms = $Term.Terms
$Ctx.Load($ChildTerms)
$Ctx.ExecuteQuery()
#Process all child terms
Foreach ($ChildTerm in $ChildTerms)
{
$Indent = $Indent +"`t"
Write-host $Indent -NoNewline
Get-SPOTerm($ChildTerm)
}
$Indent = "`t"
}
#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$TermGroupName="Deals Pipeline"
$TermsetName="Dealstage"
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the taxonomy session and termstore
$TaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx)
$TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
$Ctx.Load($TaxonomySession)
$Ctx.Load($TermStore)
#Get the Term Group
$TermGroup = $TermStore.Groups.GetByName($TermGroupName)
$Ctx.Load($TermGroup)
#Get the termset
$TermSet = $TermGroup.TermSets.GetByName($TermSetName)
$Ctx.Load($TermSet)
$Ctx.ExecuteQuery()
#Get all terms
$TermColl=$TermSet.Terms
$Ctx.Load($TermColl)
$Ctx.ExecuteQuery()
# Loop through all the terms
Foreach($Term in $TermColl)
{
#Get the terms recursively
Get-SPOTerm($Term)
}
PnP PowerShell to Get All Terms from a Term Set in SharePoint Online
Here is how to get terms from a term set using PnP PowerShell:
#Config Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Credentials (Get-Credential)
#Get All Terms from a Term Set
Get-PnPTerm -TermSet "DealStage" -TermGroup "Deals Pipeline"
Result:
The Get-PnPTerm also supports -IncludeChildTerms -Recursive parameters! Let’s export all terms of a term set to a CSV file:
#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Interactive
#Get All Terms from a Term Set
Get-PnPTerm -TermGroup "Deals Pipeline" -TermSet "Job Titles" | Select Name | Export-Csv "C:\Temp\Terms.csv" -NoTypeInformation
It is actually heplful. THank you for publishing such information.
Can you also help me with my requirement?
I want to fetch the details as in what terms have been assigned to what users.
example:- we have used Location and created India, US, UK etc… which is now populating in front of Office Location under user profiles.
Any help is appreciated
You can export user profile properties using PowerShell: How to Export User Profile Properties in SharePoint Online using PowerShell