Forum Discussion
Update Sensitivity labels for documents powershell?
Hi all,
We just migrated sites from on prem to SPO. The old sites had a Security Classification column with our classification (for example, Proprietary and Public). We have sensitivity labels implemented in SPO, and now we'd like to set the sensitivity label for each document based on what is in the Security Classification column....then remove the old security classification column.
Is there something like a powershell script to update/add a label for a document?
Thanks!
Troy
- Deleted
Yes, you can use PowerShell to update or add a sensitivity label to documents in SharePoint Online based on the values from the old Security Classification column. Here's an example of how you can accomplish this:
1. Install the required PowerShell modules:
- Install the SharePoint Online Management Shell module: `Install-Module -Name Microsoft.Online.SharePoint.PowerShell`2. Connect to SharePoint Online:
Connect-SPOService -Url "https://yourtenant-admin.sharepoint.com"
3. Retrieve the documents in the desired library or site:
$documents = Get-SPOSite -Identity "https://yourtenant.sharepoint.com/sites/yoursite" | Get-SPOList -Identity "Documents" | Get-SPOListItem -Limit All
4. Iterate through the documents and update the sensitivity label based on the Security Classification column value:
foreach ($document in $documents) { $securityClassification = $document.FieldValues["SecurityClassification"] # Map the Security Classification value to the appropriate sensitivity label $sensitivityLabel = "" switch ($securityClassification) { "Proprietary" { $sensitivityLabel = "Confidential" } "Public" { $sensitivityLabel = "General" } # Add more cases for other values if needed } if ($sensitivityLabel -ne "") { Set-SPOSensitivityLabel -SiteUrl "https://yourtenant.sharepoint.com/sites/yoursite" -ListTitle "Documents" -ListItemId $document.Id -LabelName $sensitivityLabel } }
5. After successfully updating the sensitivity labels for the documents, you can proceed to remove the old Security Classification column from the SharePoint list or library using the SharePoint Online Management Shell or the SharePoint Online Management PowerShell cmdlets.
Please note that the code above is just an example and may need to be adapted to match your specific column names, sensitivity label names, and site structure. Ensure that you have the necessary permissions to update sensitivity labels and manage the SharePoint site.
Remember to test the PowerShell script on a smaller subset of documents or in a test environment before executing it on a larger scale to avoid unintended consequences.
If I have answered your question, please mark your post as SolvedIf you like my response, please give it a like- TeeroyLyonCopper ContributorHI Shijuraj,
First, thanks for responding and providing this solution. Just want to ask...are you sure that Set-SPOSensitivityLabel is as valid command? It's not working, and the only instance I can find of that command in google, is this forum entry?- Deleted
TeeroyLyon Oops.. its only available in beta version, could you please try this graph api method mentioned in this link: https://learn.microsoft.com/en-us/graph/api/driveitem-assignsensitivitylabel?view=graph-rest-beta&tabs=http
If I have answered your question, please mark your post as SolvedIf you like my response, please give it a like
- raphael1974Copper ContributorHi Deleted I cannot find (Get-SPOList -Identity "Documents" | Get-SPOListItem -Limit All) module "Microsoft.Online.SharePoint.PowerShell`" you mentioned is installed. The command Get-SPOSite -Identity is working and available.
My intention, do set default label after migration for all documents in a defined sharepoint online site. Do you have any idea how to achieve this?.. Regards Raphi
- SerkarCopper Contributor
TeeroyLyon Dear Teeroy, yes it is possible. You need to use the API
POST /drives/{drive-id}/items/{item-id}/assignSensitivityLabel
Source: driveItem: assignSensitivityLabel - Microsoft Graph v1.0 | Microsoft Learn
I spent some time and have documented the whole process how to do it with PowerShell in my blog:
https://sposcripts.com/assign-sensitivity-labels-in-sharepoint/
Let me know if it helped you