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/
Niraj Tenany
Dec 31, 2021Copper Contributor
We have applied labels in sharepoint using MIP SDK and it has worked very well for us. While auto-classification is good, there are situations where organizations are not able to create classification rules and maintain them. We call this approach Project Based Label approach or Location based label approach. User requests a SharePoint SIte or teams and at that time we programatically create the labels and assign those labels to the site with appropriate permissions. When a document is uploaded to the site, we protect it using the label. This is an extensive topic and it took us a year to build out the complete capability but it works extremely well.
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.
- WJN78Nov 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.