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. $SPWeb =Get-SPWeb -Site $url
  6. foreach($SPList in $SPWeb.Lists){
  7. if ($SPList.BaseType -eq "DocumentLibrary")
  8. {
  9. 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")
  10. {
  11. Foreach ($SPItem in $SPList.Items)
  12. {
  13. $info = @{
  14. "Web" = $SPList.ParentWebUrl
  15. "Item Name" = $SPItem.Name
  16. "Created" = ($SPItem["Created"] -as [datetime]).DateTime
  17. "Modified" = ($SPItem["Modified"] -as [datetime]).DateTime
  18. "Created By" = $SPItem["Editor"]
  19. "Size" = $SPItem.File.Length
  20. "List Name" =$SPList.Title
  21. }
  22. New-Object PSObject -Property $Info | Select "Web","List Name","Item Name", "Created", "Modified", "Created By", "Size" | FT -AutoSize
  23. }
  24. }
  25. }
  26. }
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. Function Format-FileSize() {
  4. Param ([int]$size)
  5. If ($size -gt 1TB) {[string]::Format(“{0:0.00} TB”, $size / 1TB)}
  6. ElseIf ($size -gt 1GB) {[string]::Format(“{0:0.00} GB”, $size / 1GB)}
  7. ElseIf ($size -gt 1MB) {[string]::Format(“{0:0.00} MB”, $size / 1MB)}
  8. ElseIf ($size -gt 1KB) {[string]::Format(“{0:0.00} kB”, $size / 1KB)}
  9. ElseIf ($size -gt 0) {[string]::Format(“{0:0.00} B”, $size)}
  10. Else {“”}
  11. }
  12. $url =Read-Host "Enter the Site Collection URL"
  13. $SPWeb =Get-SPWeb -Site $url
  14. foreach($SPList in $SPWeb.Lists){
  15. if ($SPList.BaseType -eq "DocumentLibrary")
  16. {
  17. 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")
  18. {
  19. Foreach ($SPItem in $SPList.Items)
  20. {
  21. $info = @{
  22. "Web" = $SPList.ParentWebUrl
  23. "Item Name" = $SPItem.Name
  24. "Created" = ($SPItem["Created"] -as [datetime]).DateTime
  25. "Modified" = ($SPItem["Modified"] -as [datetime]).DateTime
  26. "Created By" = $SPItem["Editor"]
  27. "Size" = Format-FileSize ($SPItem.File.Length)
  28. "List Name" =$SPList.Title
  29. }
  30. New-Object PSObject -Property $Info | Select "Web","List Name","Item Name", "Created", "Modified", "Created By", "Size" | FL -Autosize
  31. }
  32. }
  33. }
  34. }