PowerShell Script to get the value of managed metadata column of document library of SharePoint Online
Hi All,
Greetings. Life is Beautiful 🙂
Exploring PowerShell continues.
Today I’m sharing a small but important script related to Managed Metadata columns.
Use case:
In our organization, a few department names have been updated. In this scenario, we need to identify documents tagged with the old term and replace with new term in the department managed metadata column.
To address this, I created a PowerShell script.
In this post/script, the focus is:
How to retrieve and validate the value of a single-value Managed Metadata column in a SharePoint document library using PowerShell, so it can be replaced if it matches the old term.
Details / Steps
- Declare required parameters
- Tenant Id
- Site URL
- ClientId
- List Name
- CertificatePath
- CertificatePassword
- Field Name
$tenant = "knowledgejunction1.onmicrosoft.com"
$siteURL = "https://knowledgejunction1.sharepoint.com/"
$ClientId = "<My Client ID>"
$CertificatePath = "C:\Prasham\m365KJ.pfx"
$CertificatePassword = "<Certificate Password>"
$ListName = "Knoweldge Junction Documents"
$FieldName="News_x0020_Channel" #Internal Name
- Check if PnP PowerShell module is installed or not using Get-Module with parameter -ListAvailable
# Check if PnP PowerShell is installed if (Get-Module -ListAvailable -Name "PnP.PowerShell") { Write-Host "Module is installed." } else { Write-Host "Module is not installed." Install-Module PnP.PowerShell -Scope CurrentUser -Force }
- Check if PnP PowerShell module is imported or not using Get-Module. If not then import the PnP PowerShell module using Import-Module
if(-not (Get-Module -Name "PnP.PowerShell"))
{
Import-Module -name "PnP.PowerShell"
}
- Connect to the the SharePoint site where our document library resides. We will use Connect-PnPOnline CMDLET
$CertificatePassword = (ConvertTo-SecureString -AsPlainText $CertificatePassword -Force)
$KJSite = Connect-PnPOnline -Url $siteURL -ClientId $ClientId
-Tenant $tenant -CertificatePath $CertificatePath
-CertificatePassword $CertificatePassword
-ReturnConnection
if($KJSite)
{
Write-Host Connected to the KJ tenant successfully
}
- Read items from the document library.
#Get List items from the list#$ListItem = Get-PnPListItem -List $ListName -Id $ItemID -Fields $FieldName -Connection $KJSite$ListItems = Get-PnPListItem -List $ListName -Fields $FieldName -Connection $KJSite
- Retrieve and print the current value of the Managed Metadata column
foreach ($ListItem in $ListItems){ #Get Managed Metadata Field Value $MMSFieldValue = $ListItem[$FieldName] Write-host "Label " $MMSFieldValue.Label Write-host "Term Guid " $MMSFieldValue.TermGuid}#
Complete Script
<#.SYNOPSIS PowerShell script to update the value of managed metadata column of a SharePoint Online document library.DESCRIPTION This script iterates through the list items of SharePoint document library Retrieves the current value of the managed metadata column Checks a value of specified managed metadata field Based on the defined condition, it replaces the existing value with a new value..NOTES Author: Prasham Sabadra Version 1.0 - initial release#>#Variables declaration / Intialization $tenant = "knowledgejunction1.onmicrosoft.com" $siteURL = "https://knowledgejunction1.sharepoint.com/" $ClientId = "<My Client ID>" $CertificatePath = "C:\Prasham\m365KJ.pfx" $CertificatePassword = "<Certificate Password>" $ListName = "Knoweldge Junction Documents" $FieldName="News_x0020_Channel" #Internal Name #region Module Check # Check if PnP PowerShell is installed if (Get-Module -ListAvailable -Name "PnP.PowerShell") { Write-Host "Module is installed." } else { Write-Host "Module is not installed." Install-Module PnP.PowerShell -Scope CurrentUser -Force } #As we are using PnP PowerShell module, check if PnP PowerShell module is imported if(-not (Get-Module -Name "PnP.PowerShell")) { Import-Module -name "PnP.PowerShell" }#endregion#region - Connect to the site for which we need to get the Reccycle Bin items size $CertificatePassword = (ConvertTo-SecureString -AsPlainText $CertificatePassword -Force) $KJSite = Connect-PnPOnline -Url $siteURL -ClientId $ClientId -Tenant $tenant -CertificatePath $CertificatePath -CertificatePassword $CertificatePassword -ReturnConnection if($KJSite) { Write-Host Connected to the KJ tenant successfully }#endregion#Get List items from the list$ListItems = Get-PnPListItem -List $ListName -Fields $FieldName -Connection $KJSite foreach ($ListItem in $ListItems){ #Get Managed Metadata Field Value $MMSFieldValue = $ListItem[$FieldName] Write-host "Label " $MMSFieldValue.Label Write-host "Term Guid " $MMSFieldValue.TermGuid}#foreach ($item in $ListItem)
Thank you for reading. Feel free to discuss anything about Microsoft 365 PowerShell.
HAVE a WONDERFUL TIME AHEAD 🙂
