Commit f82f30cf authored by Jos Lieben's avatar Jos Lieben

report on files with a certain path length in Sharepoint Online

parent f462885a
report on files that exclude a certain path length in any sharepoint or teams site in Office 365
Certain Office tools cannot access Sharepoint Online files if they exceed a certain path length. This script helps you assess which files are affected so you can remediate proactively.
The script can scan for all or specific file types. Certain modules are required and auto installed if you have sufficient permissions.
.\get-FilesWithLongPathsInOffice365.ps1 -fileExtension ".xlsx" -maxPathLength 225 -tenantName ogd -useMFA
.PARAMETER fileExtension
If you supply this parameter, only files matching this extension will be reported, if you leave it empty, all files will be reported that exceed the path length you supply
Example: .xlsx
.PARAMETER maxPathLength
Maximum length of the file path, including
Example: 220
.PARAMETER tenantName
Name of your Office 365 tenant ( = TENANTA
Example: tenanta
Switch parameter, if the admin account you plan to use is MFA enabled, supply -useMFA to this script
By default, script shows data through Out-GridView, if you supply -exportCSV, a CSV file will be written to your temp folder
filename: get-FilesWithLongPathsInOffice365.ps1
author: Jos Lieben
created: 19/10/2018
) function Load-Module{ Param( $Name ) Write-Output "Checking for $Name Module"
$module = Get-Module -Name $Name -ListAvailable
if ($module -eq $null) {
write-Output "$Name Powershell module not installed...trying to Install, this will fail in an unelevated session"
#Check if elevated
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")){
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Install-Module $Name -SkipPublisherCheck -Force -Confirm:$False
Write-Output "$Name module installed!"
write-Error "Install by running 'Install-Module $Name' from an elevated PowerShell prompt"
write-output "Module already installed"
Write-Output "loading module"
Import-Module $Name -DisableNameChecking -Force -NoClobber Write-Output "module loaded" }catch{ Write-Output "failed to load module" } } Load-Module SharePointPnPPowerShellOnline Load-Module SharePointPnPPowerShell2016 Load-Module SharePointPnPPowerShell2013 if(!$useMFA){ $Credential = Get-Credential} if($useMFA){ Connect-PnPOnline "https://$" -UseWebLogin
Connect-PnPOnline "https://$" -Credentials $Credential
$reportRows = New-Object System.Collections.ArrayList
$sites = Get-PnPListItem -List DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS -Fields ID,Title,TemplateTitle,SiteUrl,IsGroupConnected
foreach($site in $sites){
Write-Output "Processing $($site.FieldValues.Title) with url $($site.FieldValues.SiteUrl)"
if($useMFA){ Connect-PnPOnline $site.FieldValues.SiteUrl -UseWebLogin
Connect-PnPOnline $site.FieldValues.SiteUrl -Credentials $Credential
$lists = Get-PnPList -Includes BaseTemplate,EntityTypeName
$lists | where {$_.BaseType -eq "DocumentLibrary" -and ($_.EntityTypeName -eq "Documents" -or $_.EntityTypeName -eq "Shared_x0020_Documents")} | % {
Write-Output "Detected document library $($_.Title) with Id $($_.Id) and Url $($_.SiteUrl), processing..."
$items = Get-PnPListItem -List $_
foreach($item in $items){
if($item.FileSystemObjectType -eq "Folder"){
continue #Mapjes skippen we!
$baseUrl = ($site.FieldValues.SiteUrl -Split("/sites"))[0]
$relative = $item.FieldValues.FileRef
if($fileExtension -and $fileExtension.Length -gt 0 -and !$relative.EndsWith($fileExtension)){
continue #filter by file extension
$fullPath = "$($baseUrl)$($relative)"
if($fullPath.Length -lt $maxPathLength){
continue #filter by max length
$ObjectProperties = [Ordered]@{
"Path Length" = $fullPath.Length
"Site URL" = $baseUrl
"File" = $item.FieldValues.FileLeafRef
"Full Path" = $fullPath
[void]$reportRows.Add((New-Object -TypeName PSObject -Property $ObjectProperties))
$path = Join-Path $Env:TEMP -ChildPath "filesWithLongPaths.csv"
$reportRows | export-csv -Path $path -Force -NoClobber -NoTypeInformation -Encoding UTF8
Write-Output "data exported to $path"
$reportRows | Out-GridView
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment