#requires -Version 7.4.6 <# Infostruction zero-byte SharePoint file report, revision 2026-11-03. Requires PnP.PowerShell, your own Entra application ID with delegated SharePoint read permission, and interactive access to each target site. Does not modify SharePoint. Run: .\Scan0KBFiles.ps1 -SiteUrl 'https://tenant.sharepoint.com/sites/example' -ClientId 'YOUR-APP-ID' The CSV is local output, not a repair list. A zero-byte file is not proof of migration damage. #> [CmdletBinding()] param( [Parameter(Mandatory)][string[]]$SiteUrl, [Parameter(Mandatory)][guid]$ClientId, [string]$OutputPath = (Join-Path (Get-Location) ('ZeroByteFiles-' + (Get-Date -Format 'yyyyMMdd-HHmmss-fff') + '.csv')) ) function Get-ZeroByteFileRow { param([string]$SiteUrl,[string]$Library,[string]$FilePath,[string]$FileName, $ObjectType, [AllowNull()]$Length) $bytes = 0L if ($null -eq $ObjectType -or [string]$ObjectType -ne '0' -or $null -eq $Length -or -not [long]::TryParse([string]$Length,[ref]$bytes) -or $bytes -ne 0) { return } [pscustomobject][ordered]@{SiteCollectionURL=$SiteUrl;Library=$Library;FilePath=$FilePath;FileName=$FileName;Length=$bytes} } function Initialize-ZeroByteReport { param([string]$OutputPath) # New-Item without Force rejects a pre-existing report; never silently overwrite one. [void](New-Item -Path $OutputPath -ItemType File -ErrorAction Stop) '"SiteCollectionURL","Library","FilePath","FileName","Length"' | Set-Content -LiteralPath $OutputPath -Encoding UTF8 -ErrorAction Stop } function Invoke-ZeroByteScan { [CmdletBinding()] param([string[]]$SiteUrl,[guid]$ClientId,[string]$OutputPath) $ErrorActionPreference = 'Stop' if ($ClientId -eq [guid]::Empty) { throw 'Supply the client ID of your Entra application.' } Import-Module PnP.PowerShell -ErrorAction Stop $outputFile = [IO.Path]::GetFullPath($OutputPath) Initialize-ZeroByteReport -OutputPath $outputFile $found = 0; $inspected = 0 try { foreach ($site in $SiteUrl) { $uri = [uri]$site if (-not $uri.IsAbsoluteUri -or $uri.Scheme -ne 'https') { throw "Not an absolute HTTPS site URL: $site" } $connection = $null try { $connection = Connect-PnPOnline -Url $site -Interactive -ClientId $ClientId.ToString() -ReturnConnection -ErrorAction Stop $libraries = @(Get-PnPList -Connection $connection -ErrorAction Stop | Where-Object { $_.BaseTemplate -eq 101 -and -not $_.Hidden }) foreach ($library in $libraries) { Write-Host "Scanning $site / $($library.Title)" # One recursive, paged traversal per library. No separate folder recursion. $query = "1000" Get-PnPListItem -List $library.Id -Query $query -PageSize 1000 -Connection $connection -ErrorAction Stop | ForEach-Object { $inspected++ $row = Get-ZeroByteFileRow -SiteUrl $site -Library $library.Title -FilePath $_.FieldValues.FileRef -FileName $_.FieldValues.FileLeafRef -ObjectType $_.FieldValues.FSObjType -Length $_.FieldValues.File_x0020_Size if ($null -ne $row) { $row | Export-Csv -LiteralPath $outputFile -Append -NoTypeInformation -Encoding UTF8 -ErrorAction Stop $found++ } } } } finally { if ($null -ne $connection) { Disconnect-PnPOnline -Connection $connection -ErrorAction SilentlyContinue } } } } catch { throw "Scan incomplete. Preserve the partial report at $outputFile. Error: $($_.Exception.Message)" } [pscustomobject]@{Status='Completed for accessible non-hidden libraries';ItemsInspected=$inspected;ZeroByteFiles=$found;OutputPath=$outputFile} } if ($MyInvocation.InvocationName -ne '.') { $invokeParameters=@{} + $PSBoundParameters $invokeParameters.OutputPath=$OutputPath Invoke-ZeroByteScan @invokeParameters }