Get sensitivity labels using PowerShell script

Instantly Get sensitivity labels using PowerShell script in just 2 steps

One comment

 4,947 total views,  7 views today

In this “Get sensitivity labels” tutorial, we will learn how to get the sensitivity label or how to export all sensitivity labels from the given tenant using the PowerShell script. In our previous tutorial, we explained what is sensitivity label and how to activate it for your tenant.

Prerequisites to get sensitivity labels PowerShell command – Install ExchangeOnlineManagement – Sensitivity labels PowerShell

Open your PowerShell command prompt with administrator mode and run the below command:


Install-Module -Name ExchangeOnlineManagement -RequiredVersion 2.0.5

Note:

 

Get sensitivity labels using PowerShell script (sensitivity labels PowerShell)

Using the below PowerShell script, we can get all published sensitivity labels from the tenant:


####The below script is used to export all sensitivity labels from the tenant #############

cls

$PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue
if($PSshell -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$fileName = "LogExportAllSensitivityLabels"

#'yyyyMMddhhmm yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename = $enddate + '_VMReport.doc'
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
#$logPath = $directoryPath + "\" + $logFileName

$logPath = $directoryPathForLog + "\" + $logFileName

$isLogFileCreated = $False

function Write-Log([string]$logMsg)
{
if(!$isLogFileCreated){
Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
{
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
else
{
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
else
{
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}

Import-Module ExchangeOnlineManagement

try
{

Connect-IPPSSession #This will ask you to enter your Global Admin credential

#Get-Label

 Get-Label |Format-Table -Property DisplayName, Name, Guid

}
catch
{

$ErrorMessage = $_.Exception.Message +"in exporting sensitivity labels!:"
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage

}

Description of getting sensitivity labels script (sensitivity labels PowerShell)

  • Connect-IPPSSession command will ask you to enter your Global Admin credential.
  • The Get-Label command will list out all sensitivity labels from the tenant.
  • This line generates the formatted output: Get-Label |Format-Table -Property DisplayName, Name, Guid

Once we execute the above script successfully, we can see all the published sensitivity labels from the tenant.

Export Sensitivity labels using the PowerShell script
Export Sensitivity labels using the PowerShell script

Notes:

  • Once we get the sensitivity label Guid, we can apply the sensitivity label automatically by using the PowerShell script or Power Automate.

A common issue in the Get-Label command (sensitivity labels PowerShell)

We might encounter the below error while executing the Get-Label command

the term 'get-label' is not recognized as the name of a cmdlet

Solution

Install the ExchangeOnlineManagement module and import it at the beginning of the script.

Summary: Get sensitivity labels (sensitivity labels powershell)

Thus, in this article, we have learned about how to get sensitivity labels using the PowerShell script from the tenant.

See Also: Sensitivity labels SharePoint

You might be interested in the below sensitivity label article:

About Post Author

1 comments on “Instantly Get sensitivity labels using PowerShell script in just 2 steps”

Do you have a better solution or question on this topic? Please leave a comment