1

I have a SP 2013 environment with documents uploaded to the sub site Document centre. I need to know which files was uploaded and which content type (also if no content type was selected) was uploaded. So the script needs to loop through the subsite Document Centre and list all files with content type. Thanks to Amal Hashim's script I got the following: But this goes through the whole site and list content types assigned to all libraries. If someone can help me to modify it to loop only through the sub site and list files with their content type assigned and where they are saved - I would very much appreciate that:

$Site= Get-SPSite "http://dbnsrv06"
$spWebApp = $Site.WebApplication
$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 "C:/filename.csv"
1

We can use the PowerShell below to achieve it.

$ct = @()
$Web = Get-SPWeb "http://subsiteurl"
foreach($list in $Web.Lists)
{
    if($list.BaseTemplate -eq "DocumentLibrary")     
    {
        if(-not ($systemlibs -Contains $list.Title))
        {
            if ($list.AllowContentTypes -eq $true)
            {               
                foreach ($item in $list.Items)
                {
                    $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 $item.ContentType
                    $ctObject | Add-Member -Name 'FileName' -MemberType Noteproperty -Value $item.Name
                    $ctObject | Add-Member -Name 'URL' -MemberType Noteproperty -Value $item.Url                    
                    $ct += $ctObject      
                }     
            }
        }
    }
}
$ct | export-csv "C:/filename.csv"
  • The author stated that he needs list all files - this script does not collect information about files. – Kristaps Vilerts Nov 27 '18 at 10:30
  • Thank you for your effort LZ_MSFT. But the script only returns: "Style Library","Microsoft.SharePoint.SPContentType","DocCentreStyle.css","Style Library/DocCentreStyle.css". I got a script in the meantime that did the trick. – Heste Dec 3 '18 at 18:44
0

This code will iterate through all lists and all items in just one site and will work efficiently in huge lists (even hundreds of thousands of items). Please adjust line with variable $results as needed.

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) { Add-PSSnapin Microsoft.SharePoint.Powershell }

$web = Get-SPWeb "http://dbnsrv06"
foreach($list in $web.Lists)
{
    if($list.BaseTemplate -eq "DocumentLibrary")
    {
        $query = New-Object Microsoft.SharePoint.SPQuery
        $query.ViewAttributes = "Scope='Recursive'"
        $query.RowLimit = 100
        do
        {
            $items = $list.GetItems($query)
            $query.ListItemCollectionPosition = $items.ListItemCollectionPosition

            foreach($item in $items)
            {
                $result = $item.Url + " ; " + $item.ID + " ; " + $item.ContentType.Name
                $result |Add-Content -Path C:\report.txt
            }   
        } 
        while ($query.ListItemCollectionPosition -ne $null) 
    }
}
$web.Dispose();
Write-Host "End" -b DarkGreen -f Green

Update: added checking if list is "library type" - script now avoids lists and iterates through libraries (that could contain files).

  • Thanks Kristaps - just want to know - I want to see all files in all <u>Libraries</u> it seems the script look at lists – Heste Nov 27 '18 at 7:54
  • $web.Lists contains both lists and libraries, so script goes through both of them. Do you need one specific library or all library, except lists? – Kristaps Vilerts Nov 27 '18 at 9:10

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.