OneDrive–Check for Long File names

Following script will check for Long File Names,Folders and File+Folders. Any location which contains more then (249 – User OneDrive Folder Length) will be reported, This outout is stored in a Share with UserName_ComputerName Format. THis can be run as Scheduled tasks

 

$OneDriveLocationLength =  $Env:OneDrive.Length
$LengthToCheck = 249 - $OneDriveLocationLength
$SyncFile = "\\NetworkShare\OneDriveSyncissues\" + $Env:USERNAME + "-" + $Env:COMPUTERNAME + ".csv" #Replace with Network Share
$Columns = "Name" + ";" + "Type" + ";" + "IsFolderLengthLong" + ";" + "IsFileLengthLong" + ";" + "IsFolderFileLengthLong" + ";" + "Length"
$columns | out-file -filepath $SyncFile -append
$PathExclusion = @("Windows";"Program Files") # Add/Remove folders that you will like to exclude from Scan
$Drives = @("C:\" ; "D:\") #Add/Remove Drives from Scran
$FoldersToCheck = @()
foreach($Drive in $Drives)

{

    $Folders = Get-ChildItem  $Drive | Where-Object {($_.PSIscontainer -eq $true) }
    foreach ($Folder in $Folders)
            {
                    $i=0
                    $Exclude = 0
                    $Count = $PathExclusion.Count
                   
                        while($i -le $Count)
                             {
            
                                if($Folder.name -eq $PathExclusion[$i])
                                    {
                                    $Exclude = 1
                                    }
               
                                else
                                    {
                               
                                    }
                                    $i++
            
                            }
                                 if ($Exclude -eq 1)
                                 {
                                
                                 }
                                 else
                                 {
                                 $FoldersToCheck += $folder.FullName
                                 }

               }
              
              
}

 

function FileLength ($FileLengthDir)
{

$FilesForLength = @()
$FilesForLength  = Get-ChildItem  $FolderToCheck -Recurse -ErrorAction silentlycontinue | Where-Object {($_.PSIscontainer -eq $false) }

    foreach($FileForLength in $FilesForLength)
    {
        if($FileForLength -eq $null)
        {
        }
        else
        {
   
            if($FileForLength.name.length -ge $LengthToCheck)
            {
                   
            $Output = $FileForLength.Fullname + ";" + "File" + ";" + "" + ";" + "Yes" + ";" + "" + ";" + $FileForLength.name.length
            $Output | out-file -filepath $SyncFile -append
           
            }
           
            if($FileForLength.Fullname.length -ge $LengthToCheck)
            {
           
            $Output = $FileForLength.Fullname + ";" + "FolderFile" + ";" + "" + ";" + "" + ";" + "Yes" + ";" + $FileForLength.Fullname.length
            $Output | out-file -filepath $SyncFile -append
            }
           
        }   
   }

}

function FolderLength ($FolderToCheck)
{
$FoldersForLength = @()
$FoldersForLength  += Get-Item  $FolderToCheck -ErrorAction silentlycontinue | Where-Object {($_.PSIscontainer -eq $true) }
$FoldersForLength  += Get-ChildItem  $FolderToCheck -Recurse -ErrorAction silentlycontinue | Where-Object {($_.PSIscontainer -eq $true) }

    foreach($FolderForLength in $FoldersForLength)
    {
        if($FolderForLength.fullname.length -ge $LengthToCheck)
        {
       
        $Output = $FolderForLength.fullname + ";" + "Folder" + ";" + "Yes" + ";" + "" + ";" + "" + ";" + $FolderForLength.fullname.length
        $Output | out-file -filepath $SyncFile -append  
        }
        if ($FolderForLength -eq $null)
        {
        }
        else
        {
        FileLength ($FolderForLength.fullname)
        }
       
    }

}

foreach ($FolderToCheck in $FoldersToCheck)
{
FolderLength $FolderToCheck

}