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,
- Add-PSSnapin “Microsoft.SharePoint.Powershell”
- Clear-Host
- Add-PSSnapin "Microsoft.SharePoint.Powershell"
- $url =Read-Host "Enter the Site Collection URL"
-
- $SPWeb =Get-SPWeb -Site $url
-
- foreach($SPList in $SPWeb.Lists){
- if ($SPList.BaseType -eq "DocumentLibrary")
- {
- 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")
- {
- Foreach ($SPItem in $SPList.Items)
- {
-
- $info = @{
- "Web" = $SPList.ParentWebUrl
- "Item Name" = $SPItem.Name
- "Created" = ($SPItem["Created"] -as [datetime]).DateTime
- "Modified" = ($SPItem["Modified"] -as [datetime]).DateTime
- "Created By" = $SPItem["Editor"]
- "Size" = $SPItem.File.Length
- "List Name" =$SPList.Title
- }
-
- New-Object PSObject -Property $Info | Select "Web","List Name","Item Name", "Created", "Modified", "Created By", "Size" | FT -AutoSize
- }
- }
- }
- }
The result will be like below
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.
- Function Format-FileSize() {
- Param ([int]$size)
- If ($size -gt 1TB) {[string]::Format(“{0:0.00} TB”, $size / 1TB)}
- ElseIf ($size -gt 1GB) {[string]::Format(“{0:0.00} GB”, $size / 1GB)}
- ElseIf ($size -gt 1MB) {[string]::Format(“{0:0.00} MB”, $size / 1MB)}
- ElseIf ($size -gt 1KB) {[string]::Format(“{0:0.00} kB”, $size / 1KB)}
- ElseIf ($size -gt 0) {[string]::Format(“{0:0.00} B”, $size)}
- Else {“”}
- }
Once we've added and associated this function to the file size column, we need to replace the line from
- "Size" = $SPItem.File.Length
to
- "Size" = Format-FileSize ($SPItem.File.Length)
And the final script would be somewhat like the below one.
- Clear-Host
- Add-PSSnapin "Microsoft.SharePoint.Powershell"
-
- Function Format-FileSize() {
- Param ([int]$size)
- If ($size -gt 1TB) {[string]::Format(“{0:0.00} TB”, $size / 1TB)}
- ElseIf ($size -gt 1GB) {[string]::Format(“{0:0.00} GB”, $size / 1GB)}
- ElseIf ($size -gt 1MB) {[string]::Format(“{0:0.00} MB”, $size / 1MB)}
- ElseIf ($size -gt 1KB) {[string]::Format(“{0:0.00} kB”, $size / 1KB)}
- ElseIf ($size -gt 0) {[string]::Format(“{0:0.00} B”, $size)}
- Else {“”}
- }
-
- $url =Read-Host "Enter the Site Collection URL"
-
- $SPWeb =Get-SPWeb -Site $url
-
- foreach($SPList in $SPWeb.Lists){
- if ($SPList.BaseType -eq "DocumentLibrary")
- {
- 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")
- {
- Foreach ($SPItem in $SPList.Items)
- {
-
- $info = @{
- "Web" = $SPList.ParentWebUrl
- "Item Name" = $SPItem.Name
- "Created" = ($SPItem["Created"] -as [datetime]).DateTime
- "Modified" = ($SPItem["Modified"] -as [datetime]).DateTime
- "Created By" = $SPItem["Editor"]
- "Size" = Format-FileSize ($SPItem.File.Length)
- "List Name" =$SPList.Title
- }
-
- New-Object PSObject -Property $Info | Select "Web","List Name","Item Name", "Created", "Modified", "Created By", "Size" | FL -Autosize
- }
- }
- }
- }