2

I'm pretty new to PowerShell and try to collect the following data about every document library in a site-collection:

  1. Document Library Name
  2. Created Date
  3. Content Type

Are there any general improvements i could do to the code? Are there any other way of getting the name of the content type?

add-pssnapin microsoft.sharepoint.powershell -erroraction SilentlyContinue 

$siteURL = "https://contoso.com/sites/projects"
$site = Get-SPSite($siteURL)

foreach($web in $site.AllWebs) 
{
    foreach($list in $web.Lists)
    {
        if($list.BaseType -eq "DocumentLibrary")
        {
        $listName = $list.Title
        $listCreatedDate = $list.Created
        $listContentType = $list.ContentTypes
        $listContentTypeName = $listContentType.Name

        write-host "List name: $listName"
        write-host "List created: $listCreatedDate"
        write-host "List content type: $listContentTypeName"
    }
}
}
$web.Dispose();
$site.Dispose();
1
  • 2
    $list.ContentTypes returns a SPContentTypeCollection. If one of your document library contains more than one content type, $listContentType.Name will return $null. Also, if there are no content types, or if content types are not enabled, $listContentType will likely be $null Commented Oct 8, 2014 at 17:23

2 Answers 2

1

You could utilize the following pattern to inventory SharePoint objects (converted example):

Get-SPSite $siteURL |
Select -ExpandProperty AllWebs |
Select -ExpandProperty Lists | Where { $_.BaseType -eq "DocumentLibrary" } |
Select Title, Created, ContentTypes
2
  • 1
    Excellent! Clean and simple output Commented Oct 10, 2014 at 13:29
  • One thing i noticed is that while running the script in powershell, the select statement for ContentTypes is displayed correctly but while trying to export it to a csv file, it returns "Microsoft.SharePoint.SPContentTypeCollection" instead of the content type name. Commented Oct 10, 2014 at 13:58
1

Try below piece in your code.

if($list.BaseType -eq "DocumentLibrary")
{
 foreach ($ctype in $list.ContentTypes)  
 {  

        write-host "List name:" $list.Title
        write-host "List created:" $list.Created
        write-host "List content type:" $ctype.Name
  }
}

http://gallery.technet.microsoft.com/office/Find-Lists-or-Libraries-fd247709

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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