Get document library inventory report in SharePoint using PowerShell script

Get document library inventory report in SharePoint using PowerShell script

No comments

 12,042 total views,  136 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

Get document library inventory report in SharePoint using PowerShell script
Get document library inventory report in SharePoint using PowerShell script

#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:

SharePoint Document Library Inventory Report using PowerShell
SharePoint Document Library Inventory Report using PowerShell

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: