1

I have following code and it works great. However, business unit now wants to see content types for each document library. Most document libraries come with Document and Folder content types. Now, there are some libraries which has 5 or 6 different content types. I need to provide a csv with 1 row for document library (name, url, content types). The content type column should have all the content types in it. I can't get the content type exported to csv. When I do write-host on the content type (write-host $ct) I can see it has all the content types belong to a document library. I understand $ct is an array object and it may need to be handled specially in order for add-content to accept it's data. Any idea how to do this?

#For Output file generation
$OutputFN = "Libraries.csv"
#delete the file, If already exist!
if (Test-Path $OutputFN)
 {
    Remove-Item $OutputFN
 }
#Write the CSV Headers
Add-Content $OutputFN "Web URL, Site Name, Library Name, Content Types"

$systemlibs =@("Converted Forms", "Customized Reports", "Documents", "Form Templates",  
                              "Images", "List Template Gallery", "Master Page Gallery", "Pages",  
                               "Reporting Templates", "Site Assets", "Site Collection Documents", 
                               "Site Collection Images", "Site Pages", "Solution Gallery",  
                               "Style Library", "Theme Gallery", "Web Part Gallery", "wfpub")

#Get the Site collection 
$Site= Get-SPSite "http://inside.contoso.com"
$spWebApp = $Site.WebApplication
foreach($allSites in $spWebApp.Sites)
{
       #Loop through all Sub Sites
       foreach($Web in $allSites.AllWebs)
       {
        #Write-Host "-----------------------------------------------------"
        #Write-Host "Site Name: '$($web.Title)' at $($web.URL)"
        #Write-Host "-----------------------------------------------------"
        foreach($list in $Web.Lists)
        {
            if($list.BaseTemplate -eq "DocumentLibrary" -and $list.AllowContentTypes -eq $true)             
            {
                if(-not ($systemlibs -Contains $list.Title))
                {
                    if ($list.AllowContentTypes -eq $true)
                    {
                        $ct = @()
                        foreach ($contenttype in $list.ContentTypes)
                        {
                            $ctProperties = @{ContentType = $contenttype.Name}                          
                            $ctObject = New-Object PSObject -Property $ctProperties
                            #write-host $ct
                            $ct += $ctObject
                            #write-host $ct
                            #Write-Host "$($web.URL), $($web.Title), $($list.Title), $ct"                           
                        }
                        #$ct
                        write-host $ct

                    #Write-Host "$($web.URL), $($web.Title), $($list.Title), $($ct)" 
                    #$content = $web.URL + "," + $web.Title +"," + $list.Title +"," + $ct
                    #add-content $OutputFN $content
                    }
                }
            }
        }
       }
}
6

Try this

$ct = @()
$spWebApp = $Site.WebApplication
foreach($allSites in $spWebApp.Sites)
{
    foreach($Web in $allSites.AllWebs)
    {
        foreach($list in $Web.Lists)
        {
            if($list.BaseTemplate -eq "DocumentLibrary" -and $list.AllowContentTypes -eq $true)             
            {
                if(-not ($systemlibs -Contains $list.Title))
                {
                    if ($list.AllowContentTypes -eq $true)
                    {                           
                        foreach ($contenttype in $list.ContentTypes)
                        {
                            $ctObject = New-Object -TypeName PSObject
                            $ctObject | Add-Member -Name 'WebTitle' -MemberType Noteproperty -Value $web.Title
                            $ctObject | Add-Member -Name 'WebUrl' -MemberType Noteproperty -Value $web.URL
                            $ctObject | Add-Member -Name 'ListTitle' -MemberType Noteproperty -Value $list.Title                                
                            $ctObject | Add-Member -Name 'ContentType' -MemberType Noteproperty -Value $contenttype.Name   
                            $ct += $ctObject                      
                        }                           
                    }
                }
            }
        }
    }
}

$ct | export-csv "filename.csv"
  • Thanks for the reply. I will update the code. Appreciated it. – user37331 Dec 31 '14 at 20:22
  • 1
    This code from Matt works as well.... if ($list.AllowContentTypes -eq $true){ $ct = $list.ContentTypes | Select-Object -ExpandProperty Name $content = "$($web.URL), $($web.Title), $($list.Title), $($ct -join ";")" add-content $OutputFN $content } – user37331 Dec 31 '14 at 20:22

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.