1

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.

  • I do not understand. Can you please confirm: you are currently looping through all lists of type DocumentLibrary and in this libraries all items and all versions of these items. You are writing these items to an CSV file. Now you want to Loop through Lists only? Then you could check the BaseType if this is "GenericList" for custom lists or any other list type and then the rest is nearly the same. – BOG Oct 19 '15 at 18:30
  • Currently I'm looping through lists of type DocumentLibrary like you mentioned. In addition to what I just mentioned, I want to loop through Lists as well. Could you please advise? – Aspire27 Oct 19 '15 at 18:41
0

Try something like the following (i have currently no access to a SharePoint Console to test/provide full working example):

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

foreach ($item in $list.Items) {
$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 Title" = $item["Title"]
                        "URL"=$web.Site.MakeFullUrl("$($web.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
}
New-Object PSObject -Property $data | Select "List Name", "Item Title", "Version", "Created By", "Created Date", "Modified By", "Modified Date", "URL"
}

I have removed the version part.

  • Can you advise on how I can incorporate "GenericList" to the script? In addition to the Libraries I'm looping through, I want to loop through Lists as well. – Aspire27 Oct 19 '15 at 19:58
  • Duplicate your if and foreach block and replace then "DocumentLibrary" with "GenericList". After that remove the foreach($version in $item.Versions){}. But not the inside. Thats it. – BOG Oct 20 '15 at 5:58
0

I figured it out. In order to loop through data located in Document Libraries and Lists I have to remove this line:

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

and the closing tag as well. Thanks for the responses.

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.