powershellsharepoint-2010document-librarysplist

Retrieve all Items from All Lists from a site - Powershell


I have a PowerShell script that outputs all documents from all Libraries within a Site (web) scope to a CSV file listed below:

 function Get-DocInventory([string]$siteUrl) {
$site = New-Object Microsoft.SharePoint.SPSite $siteUrl
$web = Get-SPWeb "http://contoso.com/sites/Depts/HTG"
foreach ($list in $web.Lists) {
if ($list.BaseType -eq “DocumentLibrary”) {

foreach ($item in $list.Items) {
foreach($version in $item.Versions){
$data = @{
"Version" = $version.VersionLabel
                        "List Name" = $list.Title
                        "Created By" = $item["Author"]
                        "Created Date" = $item["Created"]
                        "Modified By" = $item["Editor"]
                        "Modified Date" = $item["Modified"]
                        "Item Name" = $item.File.Name
                        "URL"=$web.Site.MakeFullUrl("$($web.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
}
New-Object PSObject -Property $data | Select "List Name", "Item Name", "Version", "Created By", "Created Date", "Modified By", "Modified Date", "URL"
}
}
$web.Dispose();
}
$site.Dispose()
}
}



Get-DocInventory  | Export-Csv -NoTypeInformation -Path C:\NewOutput.csv

I have one last thing I need to do and that is, in addition to outputting all documents from all Libraries, I need to add script to pull all items from all Lists from the same site http://contoso.com/sites/Deptss/HTG as listed in the script above. Can someone please tell me how to do so? I really need someone's help on this.

Note: I know that to iterate through all list I can change the line below, which originally iterates through Document Libraries:

if ($list.BaseType -eq “DocumentLibrary”)

To this one below to iterate through Lists:

if ($list.BaseType -eq “GenericList”)

I want to loop through both Document Libraries and Lists. How would I do so?


Solution

  • As you correctly identified, the condition in this line of code is causing your script to only loop through document libraries:

    if ($list.BaseType -eq “DocumentLibrary”)
    

    To iterate through all lists regardless of type, just remove that if clause (and its corresponding opening/closing braces) entirely.