Forum Discussion

TeeroyLyon's avatar
TeeroyLyon
Copper Contributor
May 15, 2023

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

  • TeeroyLyon 

    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 Solved
    If you like my response, please give it a like
    • TeeroyLyon's avatar
      TeeroyLyon
      Copper Contributor
      HI 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?
    • raphael1974's avatar
      raphael1974
      Copper Contributor
      Hi 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

Resources