63,128 total views, 26 views today
In this blog, I will share how we can get a document library inventory report in SharePoint using the PowerShell script and export this report into a CSV file. This script will scan through all documents libraries in a site collection and generate the list item details report in a CSV file. This document library inventory PowerShell script can be used in the SharePoint on-premise versions – SharePoint 2010/2013/2016/2019.
Key Highlights
- How to get a document library inventory report in SharePoint using PowerShell script.
- How to export SharePoint document library inventory into a CSV file using PowerShell script.
PowerShell script to get document library inventory report in SharePoint 2010/2013/2016/2019

#The below script is used to generate the document library inventory for all the sites in a site collection programmatically. cls $PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue if($PSshell -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" } $fileName = "Document Library Inventory Report" #'yyyyMMddhhmm yyyyMMdd $enddate = (Get-Date).tostring("yyyyMMddhhmmss") #$filename = $enddate + '_VMReport.doc' $logFileName = $fileName +"_"+ $enddate+"_Log.txt" $invocation = (Get-Variable MyInvocation).Value $directoryPath = Split-Path $invocation.MyCommand.Path $directoryPathForLog=$directoryPath+"\"+"LogFiles" if(!(Test-Path -path $directoryPathForLog)) { New-Item -ItemType directory -Path $directoryPathForLog #Write-Host "Please Provide Proper Log Path" -ForegroundColor Red } #$logPath = $directoryPath + "\" + $logFileName $logPath = $directoryPathForLog + "\" + $logFileName $isLogFileCreated = $False function Write-Log([string]$logMsg) { if(!$isLogFileCreated){ Write-Host "Creating Log File..." if(!(Test-Path -path $directoryPath)) { Write-Host "Please Provide Proper Log Path" -ForegroundColor Red } else { $script:isLogFileCreated = $True Write-Host "Log File ($logFileName) Created..." [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg) Add-Content -Path $logPath -Value $logMessage } } else { [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg) Add-Content -Path $logPath -Value $logMessage } } function GetAllItemsFromAllDocumentLibraries() { param ( [Parameter(Mandatory=$true)] [string] $SiteURL ) $site = New-Object Microsoft.SharePoint.SPSite $SiteURL $allWebs=$site.AllWebs foreach ($oneWeb in $allWebs) { $allLists=$oneWeb.Lists foreach ($oneList in $allLists) { if ($oneList.BaseType -ne "DocumentLibrary") { continue } $allItems=$oneList.Items #Getting all items from a list. foreach ($oneItem in $allItems) { $listItemData = @{ "Site URL" = $site.Url "Web URL" = $oneWeb.Url "List Title" = $oneList.Title "Item ID" = $oneItem.ID "Item Title" = $oneItem.Title "Item URL" = $oneItem.Url "Item Created" = $oneItem["Created"] "Item Modified" = $oneItem["Modified"] "Created By" = $oneItem["Author"] "Modified By" = $oneItem["Editor"] "File Size" = $oneItem.File.Length/1KB "File Size (MB)" = $oneItem.File.Length/1MB } New-Object PSObject -Property $listItemData } } $oneWeb.Dispose(); } $site.Dispose() } try { GetAllItemsFromAllDocumentLibraries "http://server:port/" | Out-GridView #Your site collection URL GetAllItemsFromAllDocumentLibraries "http://server:port/" | Export-Csv -NoTypeInformation -Path "C:\Temp\DocumentLibraryDetails_Report.csv"; #Your site collection URL $message="The document library inventory generation has been completed successfully." Write-Host $message -BackgroundColor Green } catch { $ErrorMessage = $_.Exception.Message +"in the document library inventory generation script!: " Write-Host $ErrorMessage -BackgroundColor Red Write-Log $ErrorMessage }
Output:

Summary:
Hence, in this blog, we have seen how we can generate or get the document library inventory report from SharePoint 2010/2013/2016/2019 programmatically using the PowerShell script.
- Learned how to get document library inventory reports in SharePoint using PowerShell script.
- learned how to export the SharePoint document library inventory into a CSV file using PowerShell script.
You may also like the following SharePoint PowerShell tutorials:
- How to start SharePoint list workflow using PowerShell
- How to hide quick launch menu in SharePoint online using PnP PowerShell
- Edit user Permission is greyed Out SharePoint Online
- Get workflow inventory from SharePoint online using PowerShell CSOM
- Create a modern team site using PnP PowerShell in SharePoint
- In 2 steps convert a classic SharePoint page to modern using PnP
- SharePoint Online: Delete All Files from document library for the given date – PowerShell CSOM
- Create SharePoint online list using PnP provisioning template
- SharePoint Automation: PowerShell script to get remote server information
- Office 365: Retrieve hub sites and associated sites using PnP Powershell
- SharePoint Online Automation – O365 – Upload files to document library using PowerShell CSOM
- SharePoint Online Automation – O365 – Create multiple items in a list using PowerShell CSOM
- SharePoint Online Automation – O365 – Update document library metadata using PowerShell CSOM
You must log in to post a comment.