kwizcom banner advertisement

SharePoint Online: Find All Large Files using PowerShell

Requirement: Find all large files in SharePoint Online site collection.

How to Find Large Files in SharePoint Online?
To find large files in SharePoint Online, you can use "Storage Metrics" page.
  • Navigate to your SharePoint Online site collection >> Click on Settings Gear >> Site Settings
  • In Site Settings page, Click on "Storage Metrics" link under "Site Collection Administration" (https://crescent.sharepoint.com/_layouts/15/storman.aspx)
  • The Storage Metrics page gives you the current Storage consumption of the site. You can navigate to each site/folder objects to get the storage stats of the particular object.
    sharepoint online find large files
You can also include the OOTB "File Size" column in your document library views to get the size of individual files! Explorer view also helps!!


SharePoint Online: PowerShell to Get All Large Files in a Site Collection
Getting large files by navigating into each folder from the above Storage metrics page is cumbersome, So lets use PowerShell to fetch all larger files in a SharePoint Online site collection.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Function to find large files of the web
Function Find-SPOLargeFiles([String]$SiteURL, [Microsoft.SharePoint.Client.Folder]$Folder)
{
    Write-host -f Yellow "Processing Folder: $($SiteCollURL)$($Folder.ServerRelativeURL)"
    Try {
            $LargeFileResult = @()
            #Get all Files from the folder
            $FilesColl = $Folder.Files
            $Ctx.Load($FilesColl)
            $Ctx.ExecuteQuery()
 
            #Iterate through each file and check size
            Foreach($File in $FilesColl)
            {
                If($File.length -gt 50MB)
                {
                    $FileURL= $SiteCollURL+$File.ServerRelativeURL
                    $Result = New-Object PSObject
                    $Result | Add-Member NoteProperty FileName($File.Name)
                    $Result | Add-Member NoteProperty FileURL($FileURL)
                    $Result | Add-Member NoteProperty Size-MB([math]::Round($File.Length/1MB))
                     
                    #Add the result to an Array
                    $LargeFileResult += $Result
 
                    Write-host -f Green "Found a File '$($File.Name)' with Size $([math]::Round($File.Length/1MB))MB"

                    #Export the result to CSV file
                    $LargeFileResult | Export-CSV $ReportOutput -NoTypeInformation -Append
                }
            }
         
            #Process all Sub Folders
            $SubFolders = $Folder.Folders
            $Ctx.Load($SubFolders)
            $Ctx.ExecuteQuery()
            Foreach($Folder in $SubFolders)
            {
                #Exclude "Forms" and Hidden folders
                If( ($Folder.Name -ne "Forms") -and (-Not($Folder.Name.StartsWith("_"))))
                {
                    #Call the function recursively
                    Find-SPOLargeFiles -SiteURL $SiteURL -Folder $Folder
                }
            }
        }
    Catch {
        write-host -f Red "Error Finding Large Files!" $_.Exception.Message
    }
}

#Function to Generate Report on Large Files in a SharePoint Online Site Collection
Function Get-SPOLargeFilesRpt($SiteURL)
{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the web from given URL and its subsites
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.Load($web.Webs)
    $Ctx.executeQuery()

    #Call the function to get large files of the web
    Find-SPOLargeFiles -SiteURL $SiteURL -Folder $Web.RootFolder

    #Iterate through each subsite of the current web and call the function recursively
    foreach ($Subweb in $web.Webs)
    {
        #Call the function recursively to process all subsites underneaththe current web
        Get-SPOLargeFilesRpt($Subweb.url)
    }
}

#Config Parameters
$SiteCollURL="https://crescent.sharepoint.com"
$ReportOutput="C:\temp\LargeFilesRpt.csv" 

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }

#Call the function 
Get-SPOLargeFilesRpt $SiteCollURL
This script gets you the list of all files which are larger than 50MB into a CSV file with "File Name", "File URL" and "Size in MB" columns!
SharePoint Online: Find All Large Files using PowerShell SharePoint Online: Find All Large Files using PowerShell Reviewed by Salaudeen Rajack on November 12, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.