I'm pretty new to PowerShell and try to collect the following data about every document library in a site-collection:
- Document Library Name
- Created Date
- 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();
$list.ContentTypesreturns aSPContentTypeCollection. If one of your document library contains more than one content type,$listContentType.Namewill return$null. Also, if there are no content types, or if content types are not enabled,$listContentTypewill likely be$null