none
Do you have PowerShell script that can count documents in each site on my SharePoint Online tenant and list in a CSV? RRS feed

  • Question

  • Hi there

    Do you have PowerShell script that can count documents in each site on my SharePoint Online tenant and list in a CSV?

    Thanks so much.

    Friday, October 11, 2019 10:08 PM

Answers

  • Hi,

    We can use PnP PowerShell to achieve it, the following script for your reference.

    $SiteUrl="https://tenant.sharepoint.com"
    $ExportFile="C:\temp\AllSiteFileCount.csv"
    $cred = Get-Credential
    Connect-PnPOnline $SiteUrl -Credentials $cred
    $SiteCollections = Get-PnPTenantSite
    
    #Array to Hold Data
    $DataCollection = @()
    foreach ($SiteCollection in $SiteCollections)
    {		 		
    	Connect-PnPOnline -Url $SiteCollection.Url -Credentials $cred
    	$RootWebFiles=Find-PnPFile -Match *.*
    	$ExportItem = New-Object PSObject
    	$ExportItem | Add-Member -MemberType NoteProperty -name "Site Url" -value  $SiteCollection.Url
    	$ExportItem | Add-Member -MemberType NoteProperty -Name "File Count" -value $RootWebFiles.Count
    	$DataCollection += $ExportItem
    	$SubWebs=Get-PnPSubWebs
    	foreach($SWeb in $SubWebs)
    	{			
    		$SubWebFiles=Find-PnPFile -Match *.* -Web $SWeb.ServerRelativeUrl
    		$ExportItem = New-Object PSObject
    		$ExportItem | Add-Member -MemberType NoteProperty -name "Site Url" -value  $SWeb.Url
    		$ExportItem | Add-Member -MemberType NoteProperty -Name "File Count" -value $SubWebFiles.Count
    		$DataCollection += $ExportItem
    	}
    	
    }
    $DataCollection | Export-CSV $ExportFile -NoTypeInformation

    More information:

    Find-PnPFile

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by ran009 Friday, October 18, 2019 11:49 PM
    Monday, October 14, 2019 8:35 AM
    Moderator
  • Hi,

    Please modify the "tenant" in the script below and check if it works.

    $SiteUrl="https://tenant.sharepoint.com"
    $cred = Get-Credential
    Connect-PnPOnline $SiteUrl -Credentials $cred

    If still not works, pleas using the script below.

    $SiteUrl="https://tenant.sharepoint.com"
    Connect-PnPOnline $SiteUrl -UseWebLogin

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by ran009 Wednesday, October 16, 2019 11:32 PM
    Wednesday, October 16, 2019 9:30 AM
    Moderator

All replies

  • please check the below

    https://gallery.technet.microsoft.com/office/Get-detail-report-of-all-55d6710e


    Please remember to click Mark as Answer on the answer if it helps you

    Monday, October 14, 2019 4:06 AM
  • Hi,

    We can use PnP PowerShell to achieve it, the following script for your reference.

    $SiteUrl="https://tenant.sharepoint.com"
    $ExportFile="C:\temp\AllSiteFileCount.csv"
    $cred = Get-Credential
    Connect-PnPOnline $SiteUrl -Credentials $cred
    $SiteCollections = Get-PnPTenantSite
    
    #Array to Hold Data
    $DataCollection = @()
    foreach ($SiteCollection in $SiteCollections)
    {		 		
    	Connect-PnPOnline -Url $SiteCollection.Url -Credentials $cred
    	$RootWebFiles=Find-PnPFile -Match *.*
    	$ExportItem = New-Object PSObject
    	$ExportItem | Add-Member -MemberType NoteProperty -name "Site Url" -value  $SiteCollection.Url
    	$ExportItem | Add-Member -MemberType NoteProperty -Name "File Count" -value $RootWebFiles.Count
    	$DataCollection += $ExportItem
    	$SubWebs=Get-PnPSubWebs
    	foreach($SWeb in $SubWebs)
    	{			
    		$SubWebFiles=Find-PnPFile -Match *.* -Web $SWeb.ServerRelativeUrl
    		$ExportItem = New-Object PSObject
    		$ExportItem | Add-Member -MemberType NoteProperty -name "Site Url" -value  $SWeb.Url
    		$ExportItem | Add-Member -MemberType NoteProperty -Name "File Count" -value $SubWebFiles.Count
    		$DataCollection += $ExportItem
    	}
    	
    }
    $DataCollection | Export-CSV $ExportFile -NoTypeInformation

    More information:

    Find-PnPFile

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by ran009 Friday, October 18, 2019 11:49 PM
    Monday, October 14, 2019 8:35 AM
    Moderator
  • Hi Dennis

    I have SP Admin access on our SharePoint Online environment.
    I get following error when I ran this script. Any thoughts please?

    Thanks.

    Connect-PnPOnline : The remote server returned an error: (403) Forbidden.
    At C:\Users\jsmith\Downloads\myintranet\PowerShell\GetDocumentCount4.ps1:4 char:1
    + Connect-PnPOnline $SiteUrl -Credentials $cred
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Connect-PnPOnline], WebException
        + FullyQualifiedErrorId : System.Net.WebException,SharePointPnP.PowerShell.Commands.Base.ConnectOnline
    
    Get-PnPTenantSite : No connection, please connect first with Connect-PnPOnline
    At C:\Users\jsmith\Downloads\myintranet\PowerShell\GetDocumentCount4.ps1:5 char:20
    + $SiteCollections = Get-PnPTenantSite
    +                    ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-PnPTenantSite], InvalidOperationException
        + FullyQualifiedErrorId : System.InvalidOperationException,SharePointPnP.PowerShell.Commands.GetTenantSite
    
    Export-CSV : Could not find a part of the path 'C:\temp-manu\AllSiteFileCount.csv'.
    At C:\Users\jsmith\Downloads\myintranet\PowerShell\GetDocumentCount4.ps1:28 char:19
    + $DataCollection | Export-CSV $ExportFile -NoTypeInformation
    +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OpenError: (:) [Export-Csv], DirectoryNotFoundException
        + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

    Monday, October 14, 2019 6:27 PM
  • Hi Lakshmanan

    This script gives me details of all documents, while what I need is a Document Count for all sites/subsites.

    Any help will be appreciated.

    Thanks.

    Monday, October 14, 2019 7:19 PM
  • Hi,

    I see your error code, it return 403, please check your account permission again.

    Please use a tenant to run the PowerShell script above.

    And make sure you have the admin permission for all the site collection.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, October 15, 2019 1:31 AM
    Moderator
  • Hi Dennis

    My account has SharePoint Administrator permissions actually.

    Any thoughts please?

    Thanks.

    Tuesday, October 15, 2019 9:46 PM
  • Hi,

    Please modify the "tenant" in the script below and check if it works.

    $SiteUrl="https://tenant.sharepoint.com"
    $cred = Get-Credential
    Connect-PnPOnline $SiteUrl -Credentials $cred

    If still not works, pleas using the script below.

    $SiteUrl="https://tenant.sharepoint.com"
    Connect-PnPOnline $SiteUrl -UseWebLogin

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by ran009 Wednesday, October 16, 2019 11:32 PM
    Wednesday, October 16, 2019 9:30 AM
    Moderator
  • Hi,

    Thanks for marking my reply as answer. I’m pleased to know that the information is helpful to you. I hope you are delighted with the service we provided you.
    Here I will provide a brief summary of this post for your information.

    Requirement:

    Count documents in each site on SharePoint Online tenant and list in a CSV using PowerShell

    Solution:

    Using PnP PowerShell below to achieve it.

    $SiteUrl="https://tenant.sharepoint.com"
    $ExportFile="C:\temp\AllSiteFileCount.csv"
    $cred = Get-Credential
    Connect-PnPOnline $SiteUrl -UseWebLogin
    #Connect-PnPOnline $SiteUrl -Credentials $cred
    $SiteCollections = Get-PnPTenantSite
    
    #Array to Hold Data
    $DataCollection = @()
    foreach ($SiteCollection in $SiteCollections)
    {		 		
    	Connect-PnPOnline -Url $SiteCollection.Url -Credentials $cred
    	$RootWebFiles=Find-PnPFile -Match *.*
    	$ExportItem = New-Object PSObject
    	$ExportItem | Add-Member -MemberType NoteProperty -name "Site Url" -value  $SiteCollection.Url
    	$ExportItem | Add-Member -MemberType NoteProperty -Name "File Count" -value $RootWebFiles.Count
    	$DataCollection += $ExportItem
    	$SubWebs=Get-PnPSubWebs
    	foreach($SWeb in $SubWebs)
    	{			
    		$SubWebFiles=Find-PnPFile -Match *.* -Web $SWeb.ServerRelativeUrl
    		$ExportItem = New-Object PSObject
    		$ExportItem | Add-Member -MemberType NoteProperty -name "Site Url" -value  $SWeb.Url
    		$ExportItem | Add-Member -MemberType NoteProperty -Name "File Count" -value $SubWebFiles.Count
    		$DataCollection += $ExportItem
    	}
    	
    }
    $DataCollection | Export-CSV $ExportFile -NoTypeInformation

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Friday, October 18, 2019 7:50 AM
    Moderator