Forum Discussion
JSlei
Sep 14, 2021Brass Contributor
Apply sensitivity labels using PowerShell
Is it possible to apply sensitivity labels to documents in SharePoint (not sites or groups) using PowerShell?
- Sep 16, 2021There is no cmdlet to apply labels to individual files, afaik. Set-AIPFileLabel only works on local files. Using the MIP SDK is probably the way to go, at least until Microsoft introduces a cmdlet/Graph API endpoints.
Funnily enough we do have a cmdlet to remove labels: https://office365itpros.com/2021/03/25/decrypt-sharepoint-online-documents-graph/
Jhong3030
Nov 22, 2023Copper Contributor
Niraj Tenany Can you share your approach? we are trying to use the same approach where find files with sensitivity label X and replace it with sensitivity label Y.
WJN78
Nov 22, 2023Copper Contributor
Here is an example:
# Define the folder path and label IDs
$folderPath = "C:\temp\fileswithlabels"
$currentLabelId = "<Current_Label_GUID>" # Replace with the GUID of label X
$newLabelId = "<New_Label_GUID>" # Replace with the GUID of label Y
# Iterate over each file in the folder
Get-ChildItem -Path $folderPath -File | ForEach-Object {
$file = $_.FullName
# Check if the current file has the target label
$fileStatus = Get-AIPFileStatus -Path $file
if ($fileStatus.LabelId -eq $currentLabelId) {
try {
# Apply the new label
Set-AIPFileLabel -Path $file -LabelId $newLabelId
Write-Host "Label changed for file: $file"
} catch {
Write-Host "Failed to change label for file: $file"
}
}
}
Requirement: AIPService Module for Powershell.