I am using the script below to export a 6000+ item Sharepoint list to CSV. It works great, however, 1 of the columns on the list is a Managed Metadata column. When exported with the script below, the values are all 'Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValue'.

I have tried a few variations but I am struggling. Please could anyone help me to change the script below to work with the Managed Metadata column?

Function GetAllListItems($ctx,$listUrl)
   #Get the List
    $List = $Ctx.Web.GetList($ctx.Web.ServerRelativeUrl + "/" + $listUrl)
    #Define Query to get List Items in batch
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = @"
    <View Scope='RecursiveAll'>
            <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        <RowLimit Paged="TRUE">5000</RowLimit>

    $allListItems = @()
    #Get List Items in Batch
        $ListItems = $List.GetItems($Query)
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

       foreach($listItem in $ListItems)
            $allListItems += $listItem

    While($Query.ListItemCollectionPosition -ne $null)

    Return $allListItems


Function MainProcress()

    $siteUrl = "SITE URL HERE"
    $listUrl = "Lists/List Name Goes Here"
    $outputFilePath = "F:\exportedlist.csv"

    $includeHiddenField = $False
    $includeReadonlyField = $False

    Connect-PnPOnline -Url $siteUrl -Interactive

    $ctx = Get-PnPContext

    $listFields = Get-PnPField -List $listUrl |? { $_.Hidden -eq $includeHiddenField -AND $_.ReadOnlyField -eq $includeReadonlyField} | Select -ExpandProperty internalname

    $count = 0

    $listItems= GetAllListItems -ctx $ctx -listUrl $listUrl


    # Loop through the list items  
    foreach($listItem in $listItems)  
    {   $count = $count+1
        Write-Progress -Activity "Exporting" -Status "$($count/$listItems.Count*100)% Complete:" -PercentComplete $($count/$listItems.Count*100)
        $obj=New-Object PSObject              

        $listItem.FieldValues.GetEnumerator() | Where-Object { $_.Key -in $listFields }| ForEach-Object{ 
                    if($_.Value.LookupValue){$obj | Add-Member Noteproperty $_.Key $_.Value.LookupValue}
                    else{$obj | Add-Member Noteproperty $_.Key $_.Value}                                        
     $hashtable | export-csv $outputFilePath -NoTypeInformation  



1 Answer 1


I'd create a new object for each list item in the loop and fill it with data I need.

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.