Bronze Award WinnerBronze Award Winner

Table of Contents

So at work we had an interesting situation where a client had changed names. We have our term store set up in away that we use a code to know who's who when uploading documents. Users are required to add these tags to the documents so we can filter them easily using key filters and also not have them use folders, as we all know how much folders suck.

The issue with this is, we've got hundreds of codes that need to be changed now, and only certain parts of it need to be changed. We could have got a temp to change them manually but where's the fun in that. Step in PowerShell.


For this task we needed to replace the first 3 letters on of a term that met a certain requirement of the first 3 letters and only swap them for something new. Now you can modify what I have here to meet anything you need depending on what you need. Again this is a starting point.


As with all SharePoint scripts you need to add the snap in. I personally like to make it a function and save a copy as a template and just use that when making new scripts:

#The function below installs the PowerShell Snap-ins for SharePoint. This will only run if its not already been done.
function Install-SharePoint
    IF ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
    Add-PsSnapin Microsoft.SharePoint.PowerShell

After this we need to connect to the term store itself. Now this can vary a bit depending on how you have your Term Store set up:

function Get-TermStore
    #call the adding of PSSnap-in
    #Connect to Central Admin
    $taxonomySite = get-SPSite http://site
    #Connect to Term Store in the Managed Metadata Service Application
    $taxonomySession = Get-SPTaxonomySession -site $taxonomySite
    $termStore = $taxonomySession.TermStores["TermStore"]
    #Connect to the Group and Term Set
    $termStoreGroup = $termStore.Groups["TS GROUP"]
    #Gets TermSet and lists the Terms under it by Name
    $termSet = $termStoreGroup.TermSets["TERMSET IN GROUP"]
    #Call rename function

Now here is where we're going to do that fun stuff and mess with the actual term itself:

function Rename-Term{
#Get all terms in a termSet
$allTerms = $termSet.getAllTerms()
foreach($term in $allTerms)
      $name = $
      $id = $
      #I only want all terms that are more than 6 characters in length as I am going to check for the first 6 letters                    
      if($name.length -lt 6)
           $code = $name.substring(0,6)
            #using Regex I want to know if the term starts with 3 letters and has 3 numbers after               
            if($code -match '[A-Z]{3}[0-9]{3}')
                if($code.substring(0,3) -eq $initials)
     #Using Regex I want the first 3 letters from the name and I am going to change them (replace) with my new value
                    $newName = $name -replace '^[A-Z]{3}', $newText
                    write-host "current name: $name"
                    write-host "GUID: $ID"
                    $ = $newName
                    write-host "New name: $newName" -foreground "green"
                    write-host "GUID: $ID" -foreground "green"
                    write-host ""

You may have noticed two values here $initials and $newText. You can create these as parameters so when you run the script you can add them to the command line so it runs with them. But for now I put them hard coded onto the script itself.