Copilot for Microsoft 365 Tech Accelerator
Feb 28 2024 07:00 AM - Feb 29 2024 10:30 AM (PST)
Microsoft Tech Community
SOLVED

Apply sensitivity labels using PowerShell

Brass Contributor

Is it possible to apply sensitivity labels to documents in SharePoint (not sites or groups) using PowerShell?

23 Replies

@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.

@Jhong3030 

 

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.   

 

@Vasil Michev I acknowledge that this is old thread.

 

I have 400+ local word docs that I need to apply label to.

 

I have done that via set-APIfilelabel cmdlet but when I open those docs in words , its not adding label to the header like it does if I was to manually open file and apply label.

 

Is there a way to user PowerShell to apply label and also display for word docs?

 

Regards,

Hari