Getting The List Of Documents From The Site Collection/Sub-Site Library Using PowerShell

Extract the document's from the SharePoint using Powershell

Today, I am going to show you how to extract the list of documents available in the Site collection using PowerShell.

Here, I am having one web application with one site collection and two subsites in that. One subsite is created with unique permission and another one has the inheritance permission from the parents.
 
Then, I have uploaded some documents in those two subsites as well as in the site collection. After this, I have set a unique permission to some document to check whether it's listing in the reports or not.
 
So, I am staring the script in a traditional way to import the SharePoint modules to the PowerShell ISE,
  1. Add-PSSnapin “Microsoft.SharePoint.Powershell”    
  2. Clear-Host    
  3. Add-PSSnapin "Microsoft.SharePoint.Powershell"    
  4. $url =Read-Host "Enter the Site Collection URL"    
  5.     
  6. $SPWeb =Get-SPWeb -Site $url    
  7.     
  8.     foreach($SPList in $SPWeb.Lists){    
  9.     if ($SPList.BaseType -eq "DocumentLibrary")    
  10.     {     
  11.     if ($SPList.Title -ne "Web Part Gallery" -and $SPList.Title -ne "Master Page Gallery" -and $SPList.Title -ne "Site Pages" -and $SPList.Title -ne "Theme Gallery" -and $SPList.Title -ne "Style Library")    
  12.     {    
  13.     Foreach ($SPItem in $SPList.Items)        
  14.     {    
  15.             
  16.         $info = @{     
  17.         "Web" = $SPList.ParentWebUrl    
  18.         "Item Name" = $SPItem.Name    
  19.         "Created" = ($SPItem["Created"] -as [datetime]).DateTime    
  20.         "Modified" = ($SPItem["Modified"] -as [datetime]).DateTime    
  21.         "Created By" = $SPItem["Editor"]    
  22.         "Size" = $SPItem.File.Length    
  23.         "List Name" =$SPList.Title    
  24.         }    
  25.         
  26.     New-Object PSObject -Property $Info | Select "Web","List Name","Item Name""Created""Modified""Created By""Size" | FT -AutoSize    
  27.    }    
  28.   }    
  29.  }    
  30. }    
The result will be like below
 
Result 

In the above image, you can see the file size as some numerical. Now, we need to change it in a reasonable view to make the end users understand it. 

I have found this wonderful function on the internet and I added that to my script.

  1. Function Format-FileSize() {    
  2. Param ([int]$size)    
  3.    If ($size -gt 1TB) {[string]::Format(“{0:0.00} TB”, $size / 1TB)}    
  4.    ElseIf ($size -gt 1GB) {[string]::Format(“{0:0.00} GB”, $size / 1GB)}    
  5.    ElseIf ($size -gt 1MB) {[string]::Format(“{0:0.00} MB”, $size / 1MB)}    
  6.    ElseIf ($size -gt 1KB) {[string]::Format(“{0:0.00} kB”, $size / 1KB)}    
  7.    ElseIf ($size -gt 0) {[string]::Format(“{0:0.00} B”, $size)}    
  8.    Else {“”}    
  9. }   

Once we've added and associated this function to the file size column, we need to replace the line from

  1. "Size" = $SPItem.File.Length  
to
  1. "Size"  = Format-FileSize ($SPItem.File.Length)  
Result
 
And the final script would be somewhat like the below one.
  1. Clear-Host    
  2. Add-PSSnapin "Microsoft.SharePoint.Powershell"    
  3.     
  4. Function Format-FileSize() {    
  5.    Param ([int]$size)    
  6.    If ($size -gt 1TB) {[string]::Format(“{0:0.00} TB”, $size / 1TB)}    
  7.    ElseIf ($size -gt 1GB) {[string]::Format(“{0:0.00} GB”, $size / 1GB)}    
  8.    ElseIf ($size -gt 1MB) {[string]::Format(“{0:0.00} MB”, $size / 1MB)}    
  9.    ElseIf ($size -gt 1KB) {[string]::Format(“{0:0.00} kB”, $size / 1KB)}    
  10.    ElseIf ($size -gt 0) {[string]::Format(“{0:0.00} B”, $size)}    
  11.    Else {“”}    
  12. }    
  13.     
  14. $url =Read-Host "Enter the Site Collection URL"    
  15.     
  16. $SPWeb =Get-SPWeb -Site $url    
  17.     
  18.     foreach($SPList in $SPWeb.Lists){    
  19.     if ($SPList.BaseType -eq "DocumentLibrary")    
  20.     {     
  21.     if ($SPList.Title -ne "Web Part Gallery" -and $SPList.Title -ne "Master Page Gallery" -and $SPList.Title -ne "Site Pages" -and $SPList.Title -ne "Theme Gallery" -and $SPList.Title -ne "Style Library")    
  22.     {    
  23.     Foreach ($SPItem in $SPList.Items)        
  24.     {    
  25.             
  26.         $info = @{     
  27.         "Web" = $SPList.ParentWebUrl    
  28.         "Item Name" = $SPItem.Name    
  29.         "Created" = ($SPItem["Created"] -as [datetime]).DateTime    
  30.         "Modified" = ($SPItem["Modified"] -as [datetime]).DateTime    
  31.         "Created By" = $SPItem["Editor"]    
  32.         "Size"  = Format-FileSize ($SPItem.File.Length)    
  33.         "List Name" =$SPList.Title    
  34.         }    
  35.         
  36.     New-Object PSObject -Property $Info | Select "Web","List Name","Item Name""Created""Modified""Created By""Size"  | FL -Autosize  
  37.        }    
  38.      }    
  39.   }    
  40. }