SharePoint Add Terms to Termstore via Powershell

Powershell Script

And another cool thing you can do with Powershell: Create Terms and Termgroups with Powershell. I just post about a simple way, but you can use it to build a function or a script which imports a csv or txt file.

[sourcecode language=”csharp”]

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Connect to the Metadata Service with central admin url
$taxSite = get-SPSite “http://server:port/”
$taxonomySession = Get-SPTaxonomySession -site $taxSite
$termStore = $taxonomySession.TermStores[“Managed Metadata Service”]

#Check if Group already exists
if($termStore.Groups[“MyGroup”] -eq $null)
{
$termStoreGroup = $termStore.CreateGroup(“MyGroup”)
$termStoreGroup.Description = “My Termstore für Anything”
$termStoreGroup.AddGroupManager(“domain\user”)
$termStoreGroup.AddContributor(“domain\user”)
}
else
{
$termStoreGroup = $termStore.Groups[“MyGroup”]
write-host “Group exists already”
}
if($termStoreGroup.TermSets[“MyTermSet”] -eq $null)
{
$termSet = $termStoreGroup.CreateTermSet(“MyTermSet”)

}
else
{
$termSet = $termStoreGroup.TermSets[“MyTermSet”]
write-host “TermSet exists already”
}

#Create the Terms out of this list
$termsList = “Value1;Value2;Value3;Value4”
$termsArray = $termsList.Split(“;”)

foreach($termAdd in $termsArray)
{
#Create Term
if($termSet.Terms[$termAdd] -eq $null)
{
$term = $termSet.CreateTerm($termAdd, 1033)
#$term.SetDescription(“This is a test”, 1033)
#$term.CreateLabel(“This is a test synonym”, 1033, $false)

write-host “Term $termAdd was added”
}
else
{
write-host “Term $termAdd was already added”
}
}
#Update the Term Store
$termStore.CommitAll()

[/sourcecode]

The $termslist could also be the csv or txt-file which imports the terms. Just see this as a basic script.

Hope it helps, cause it is really cool.

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

2 Comments

  1. can you pls provide the script for creatng MMS and apppool , proxy using ps?
    i tried with the below code, it failed to work, pls help
    Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

    $MetadataServiceName =”Dev MMS Application1″
    $MetadataServiceProxyName =”Dev MMS Application Proxy1″

    Write-Host “Creating Metadata Service Application – $MetadataServiceName …”

    ####################### Creating App Pools ################################
    $Server=”BLRKEC220909S”
    $dbserver = “BLRKEC220909S\SharePoint”
    $MMSDBName= “Dev MMS Application DB1″
    $MMDaccount=”domainn\user111”
    $strAppPoolName= “MMS2APPPOOL”

    $mmdAppPool = Get-SPServiceApplicationPool -Identity $strAppPoolName -EA 0

    if($mmdAppPool -eq $null)
    {
    Write-Host “Creating Managed Metadata Application Pool…”

    ## Managed Account
    $ManagedAccountGen = Get-SPManagedAccount | Where-Object {$_.UserName -eq $MMDaccount}
    If ($ManagedAccountGen -eq $NULL) { throw ” – Managed Account not found” }
    ## App Pool
    Write-Host -ForegroundColor White ” – Getting Hosted Services Application Pool, creating if necessary…”
    $mmdAppPool = Get-SPServiceApplicationPool -Identity $strAppPoolName -ea SilentlyContinue

    If ($mmdAppPool -eq $null)
    {
    $mmdAppPool = New-SPServiceApplicationPool $strAppPoolName -Account $ManagedAccountGen

    If (-not $?) { throw ” – Failed to create the application pool” }
    }
    Write-Host “Created Managed Metadata Application Pool successfully…”
    else
    {
    Write-Host “App pool already exists”
    }
    }
    #Pause

    Get-SPServiceApplicationPool
    #Write-Host “Please wait till 10 mins before proceeding further…”
    #Pause

    Write-Host “Creating Metadata Service and Proxy…”
    New-SPMetadataServiceApplication -Name $MetadataServiceName -ApplicationPool $mmdAppPool -DatabaseServer $dbserver -DatabaseName $MMSDBName > $null #-erroraction SilentlyContinue
    New-SPMetadataServiceApplicationProxy -Name $MetadataServiceProxyName -DefaultProxyGroup -ServiceApplication $MetadataServiceName > $null #-erroraction SilentlyContinue

    #Get-SPServiceInstance -server $Server | where-object {$_.TypeName -eq “Managed Metadata Web Service”} | Start-SPServiceInstance > $null

    #Get-SPServiceApplication

  2. Hi,
    Thank you for the code. It works but for the first term level only. Could you please help with that code that includes terms at Level 2 or 3?

    Emml

Leave a Reply to samolpp Cancel reply

Your email address will not be published.


*