Metadata in SharePoint is crucial for organizing, categorizing, and retrieving content efficiently. PnP PowerShell provides a robust way to automate metadata management, including creating term sets, adding terms, managing site columns, and updating metadata values across documents and lists.
This guide will walk you through automating SharePoint Metadata Management step by step using PnP PowerShell.
Prerequisites
Before you begin, ensure you have the following:
- PnP PowerShell Installed
If not installed, run:Install-Module -Name PnP.PowerShell -Force -AllowClobber
- Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive
- Replace
yourtenant
with your actual SharePoint tenant name. - The
-Interactive
parameter allows for MFA authentication.
- Replace
Step 1: Retrieve Existing Metadata (Term Store, Site Columns, Content Types)
Get All Term Groups in the Term Store
Get-PnPTermGroup
- Lists all term groups available in the Term Store.
Get Term Sets from a Specific Term Group
Get-PnPTermSet -Group "Enterprise Metadata"
- Replace
"Enterprise Metadata"
with your actual term group name.
Get Terms from a Term Set
Get-PnPTerm -TermSet "Project Categories" -Group "Enterprise Metadata"
- Replace
"Project Categories"
and"Enterprise Metadata"
with actual values.
Get All Site Columns
Get-PnPField
- Retrieves all site columns in SharePoint.
Step 2: Create and Manage SharePoint Metadata
Create a New Term Group
New-PnPTermGroup -Name "Project Metadata" -Description "Metadata for project categorization"
Create a Term Set within the Group
New-PnPTermSet -Name "Project Types" -Group "Project Metadata" -Lcid 1033
1033
is the LCID (Language Code ID) for English.
Add Terms to a Term Set
New-PnPTerm -TermSet "Project Types" -Group "Project Metadata" -Name "Infrastructure" -Lcid 1033
New-PnPTerm -TermSet "Project Types" -Group "Project Metadata" -Name "Software Development" -Lcid 1033
New-PnPTerm -TermSet "Project Types" -Group "Project Metadata" -Name "Marketing Campaign" -Lcid 1033
- This adds three terms under the “Project Types” term set.
Create a Managed Metadata Site Column
Add-PnPField -DisplayName "Project Category" -InternalName "ProjectCategory" -Group "Custom Columns" -Type TaxonomyFieldType
- Managed Metadata Columns are stored as
TaxonomyFieldType
.
Associate the Site Column with a Term Set
First, retrieve the Term Set ID:
$termSet = Get-PnPTermSet -Group "Project Metadata" -Name "Project Types"
$termSet.Id
Then, associate it with the column:
Set-PnPTaxonomyField -Identity "ProjectCategory" -TermSetId $termSet.Id
Step 3: Apply Metadata to Lists and Libraries
Add a Managed Metadata Column to a SharePoint List
Add-PnPField -List "Documents" -DisplayName "Project Category" -InternalName "ProjectCategory" -Type TaxonomyFieldType
Set Metadata for a List Item
Set-PnPListItem -List "Documents" -Identity 1 -Values @{"Project Category"="Infrastructure"}
- This assigns the “Infrastructure” metadata value to item ID
1
in the Documents library.
Bulk Update Metadata for Multiple Documents
$docs = Get-PnPListItem -List "Documents"
foreach ($doc in $docs) {
Set-PnPListItem -List "Documents" -Identity $doc.Id -Values @{"Project Category"="Software Development"}
}
- This applies the “Software Development” category to all documents in the library.
Step 4: Automate Metadata Management with a PowerShell Script
Complete Script to Automate Metadata Management
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -Interactive
# Create a New Term Group
New-PnPTermGroup -Name "Project Metadata" -Description "Metadata for projects"
# Create a New Term Set
New-PnPTermSet -Name "Project Types" -Group "Project Metadata" -Lcid 1033
# Add Terms to the Term Set
$terms = @("Infrastructure", "Software Development", "Marketing Campaign")
foreach ($term in $terms) {
New-PnPTerm -TermSet "Project Types" -Group "Project Metadata" -Name $term -Lcid 1033
}
# Create a Managed Metadata Site Column
Add-PnPField -DisplayName "Project Category" -InternalName "ProjectCategory" -Group "Custom Columns" -Type TaxonomyFieldType
# Get Term Set ID and Associate with Site Column
$termSet = Get-PnPTermSet -Group "Project Metadata" -Name "Project Types"
Set-PnPTaxonomyField -Identity "ProjectCategory" -TermSetId $termSet.Id
# Add Managed Metadata Column to Documents Library
Add-PnPField -List "Documents" -DisplayName "Project Category" -InternalName "ProjectCategory" -Type TaxonomyFieldType
# Apply Metadata to All Documents
$docs = Get-PnPListItem -List "Documents"
foreach ($doc in $docs) {
Set-PnPListItem -List "Documents" -Identity $doc.Id -Values @{"Project Category"="Infrastructure"}
}
Write-Host "Metadata Management Automated Successfully!"
Step 5: Verify and Monitor Metadata Updates
After running the script:
- Go to SharePoint Online → Documents Library
- Check if the “Project Category” metadata column is created and populated correctly.
- Navigate to Term Store Management in SharePoint Admin Center to confirm that terms are added.
Troubleshooting Common Issues
Issue 1: PnP PowerShell Commands Not Recognized
Run the following to update your module:
Update-Module -Name PnP.PowerShell
Issue 2: Term Set Not Found
Ensure the term set name and group match exactly:
Get-PnPTermSet -Group "Project Metadata"
Issue 3: Metadata Not Applied to Documents
Try re-running the metadata assignment script after verifying the field exists:
Get-PnPField -List "Documents"