2

I want to get all documents (and only documents) from a site with a Powershellscript and save the result in an .csv

I need this because a lot of documents are stored in a bunch of subfolders in the libraries and I want to restructure their teamsite.

Tried this script but can not limit it to e.G. /teams/it subsite.

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
    [System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
    [System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
    [System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

    function Get-DocInventory([string]$siteUrl) {
    $site = New-Object Microsoft.SharePoint.SPSite $siteUrl
    foreach ($web in $site.AllWebs) {
    foreach ($list in $web.Lists) {
    if ($list.BaseType -ne “DocumentLibrary”) {
    continue
    }

    foreach ($item in $list.Items) {
    $data = @{
    "Site" = $site.Url
    "Web" = $web.Url
    "list" = $list.Title
    "Item ID" = $item.ID
    "Item URL" = $item.Url
    "Item Title" = $item.Title
    "Item Created" = $item["Created"]
    "Item Modified" = $item["Modified"]
    "Created By" = $item["Author"]
    "Modified By" = $item["Editor"]
    "File Size" = $item.File.Length/1KB
    "File Size (MB)" = $item.File.Length/1MB
    }
    New-Object PSObject -Property $data
    }
    }
    $web.Dispose();
    }
    $site.Dispose()
    }

Tried to add $siteUrl = http://sp13/teams/it but it still searches over the whole SharePoint.

UPDATE:

The script I ended with:

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

function Get-DocInventory([string]$siteUrl) {
$site = New-Object Microsoft.SharePoint.SPSite $siteUrl

$web = Get-SPWeb -Identity "http://sp13/teams/it"

foreach ($list in $web.Lists) {
if ($list.BaseType -ne “DocumentLibrary”) {
continue
}

foreach ($item in $list.Items) {
$data = @{
 "list" = $list.Title
 "Item URL" = $item.Url
 "Item Title" = $item.Title
 "Item Created" = $item["Created"]
 "Item Modified" = $item["Modified"]
 "Created By" = $item["Author"]
 "Modified By" = $item["Editor"]
 "File Size" = $item.File.Length/1KB
 "File Size (MB)" = $item.File.Length/1MB
}
New-Object PSObject -Property $data
}
}
$web.Dispose();

$site.Dispose()
}

Get-DocInventory "http://sp13/teams/it" | Out-GridView
Get-DocInventory "http://sp13/teams/it" | Export-Csv -NoTypeInformation -Path "c:\temp\Document_Detail_Report_IT.csv"
  • You have me confused. "I want to get all documents (and only documents)" and ($list.BaseType -ne “DocumentLibrary”) is telling powershell to not iterate through any lists that are a document library.. would you not be missing out on a lot of documents? – Christoffer Sep 28 '15 at 14:57
  • @user19952, I assumed the same, but as I tried "if ($list.BaseType -eq “DocumentLibrary”)", I only had a couple of items. So I guess it is "kind of" what I am looking for... – Patrick Sep 28 '15 at 15:12
  • The "continue" here will skip the current loop iteration and continue to the next item in the looping. Here, if it's not document library, it will skip and continue to the next item in the list collection. Another way would have been to filter the list collection to only get the document libraries. something like that : ...in $web.Lists | Where-Object {$_.BaseType -eq "DocumentLibrary"} – JayHell Sep 28 '15 at 17:23
3

You are still getting all the SPWeb object from the site collection ($site.AllWebs)

Try getting the subsite only with GET-SPWeb and remove the first foreach

$web = Get-SPWeb -Identity "http://sp13/teams/it"
foreach ($list in $web.Lists) {
...
}
  • Great, JayHell. I knew, that I was almost there ;) Thx, also updated my question with the script I ended with. – Patrick Sep 28 '15 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.