Office 365 – PowerShell script to import all the term groups using PnP PowerShell

Hi All,

Today we will discuss how to import all our term sets in our SharePoint tenant using PnP PowerShell. PnP community provides us the PowerShell cmdlet “ImportPnPTermGroupFromXml” – which enables us to import term groups from XML file.

Here in following PowerShell script we will import all our termsets using XML file.

Following is the XML file which we will use to import:

<pnp:TermGroups xmlns:pnp="">
  <pnp:TermGroup Name="KnowledgeJunction" ID="FB840141-DD9F-4A3F-AEE8-AEA673A110DE" Description="" UpdateBehavior="Overwrite">
      <pnp:TermSet Name="Language" ID="00BF0C40-661C-4688-8E6A-89A3336230AE" Description="" IsAvailableForTagging="false">
          <pnp:Term Name="English" ID="CED32FB7-F33C-4CE1-B31A-1F0557A8A044" Owner="i:0#.f|membership|" Description="" SourceTermId="CED32FB7-F33C-4CE1-B31A-1F0557A8A044">
            <pnp:Terms />
        <pnp:Term Name="Spanish" ID="DBA9A980-DD6C-471E-979F-BE157E79C71A" Owner="i:0#.f|membership|" Description="" SourceTermId="DBA9A980-DD6C-471E-979F-BE157E79C71A">
            <pnp:Terms />

Parameters which we need for PowerShell script are

  1. XML file path – We will need XML file path from which we need to import our termsets.
  2. Credential file path – Text file which contains two lines having UserName and Password. This is just so that we will not hardcode user name and password anywhere. Also then we can make tenant specific credential file.

PowerShell script:

 Script to import the termsets from XML

.PARAMETER CredentialFilePath
  Office 365 system account credential file path having two lines in following format

  SharePoint Online Admin Site URL

   XML file Path from where we need to import the TermSets.

    [parameter(Mandatory=$false)][string]$LogFolderPath = "c:\"

if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
	Add-PSSnapin Microsoft.SharePoint.PowerShell;

Import-Module MSOnline
Import-Module Microsoft.Online.SharePoint.PowerShell

# add SharePoint CSOM libraries on given path
Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll'

Function LogWrite
   Param ([string]$logstring)
   $Logfile = $LogFolderPath + "\log.txt"
   Add-content $Logfile -value $logstring

Try {

    LogWrite "Importing the TermSets"

    #Get the user credential file path and getting user from it
    $user = Get-Content $CredentialFilePath | Select-Object -First 1

    #Getting password
    $password = Get-Content $CredentialFilePath | Select-Object -First 1 -Skip 1
    $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force

    #Credential object
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $securePassword

    #Connect to Office 365
    Connect-PnPOnline  -Credentials $credential -Url $SpoAdminUrl
    LogWrite "Connected to Office 365"

    #Importing the xml file 
    Import-PnPTermGroupFromXml -Path $XMLFilePath -Verbose
    LogWrite "XML file imported"
Catch {
   LogWrite $Error


Thanks for reading πŸ™‚

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles

This site uses Akismet to reduce spam. Learn how your comment data is processed.