I'm new to powershell and trying to loop through all document libraries of a site using below code:

$webs = (Get-SPSite "XXX" | Get-SPWeb -Limit all -ErrorAction SilentlyContinue)

$Lists = $webs.Lists

$ListItemCollection = @()

Foreach($List in $Lists) {

    if($List.BaseType -eq "DocumentLibrary"){
        $list.Items | Where-Object { $_["Choice Column"] -eq $null } | foreach {

            $ExportItem = New-Object PSObject
            $ExportItem | Add-Member -MemberType NoteProperty -name "URL" -value $_.URL
            $ExportItem | Add-Member -MemberType NoteProperty -name "Name" -value $_["Name"]
            $ListItemCollection += $ExportItem

$ListItemCollection | Export-CSV "D:\Test\Logs.txt" -NoTypeInformation

The output in txt file contains those files also which doesn't have the custom column(Choice Column). Do I need to check if the Document library contains the custom field and if so how can i check it.

Please note we are using Sharepoint 2016 on-premise.


1 Answer 1


I think the best solution to retreive all document one a site or more is using search

Use ContentTypeId and path in your query

param([string]$siteCollectionUrl="http://mySiteUrl" ,

$keyword="ContentTypeId:0x010100020D149374FFC4DF792F30EFC030C9D01* ", $rowLimit=500, $SelectProperties="Title,SPWebUrl,Path" )

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$site = Get-SPSite $siteCollectionUrl $keywordQuery = New-Object Microsoft.Office.Server.Search.Query.KeywordQuery($site);

#set request $keywordQuery.QueryText = $keyword

#set managed properties to retreive foreach($mnp in $SelectProperties.Split(",")) { $keywordQuery.SelectProperties.Add($mnp.Trim()); } $keywordQuery.RowLimit = $rowLimit

$searchExec = New-Object Microsoft.Office.Server.Search.Query.SearchExecutor $searchResults = $searchExec.ExecuteQuery($keywordQuery)

#display results $table = $searchResults.Table $table | Select Title, SPWebUrl, Path


Powershell script helper

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.