Use the code below to recursively count the folders, subfolders, and nested files. At the time I put this together SharePoint's Sync tool did not support syncing more than 5000 items per library.

In the event you have a library with more than 5000 items, then you'll want to know exactly how many root folders in the library should you move to a new library or archive library so that you can take advantage of the sync tool again.

<code>$folders = ls '\\insidepowershell.sharepoint.com@SSL\sites\Coding\Scripts' | select fullname | sort
$val = 0
while($val -le 4000)
{
Foreach ($folder in $folders){
$folder.fullname
$fcount = (ls $folder.fullname -Recurse).count 
$val = $val + $fcount 
"Innerloop Value: $val "
}
"The value is now: $val"
}