Using PowerShell to extract SharePoint ListItems and Export into CSV

Whizkidgps 0 Reputation points
2023-04-19T14:24:12.18+00:00

I have been trying to export SharePoint List Items in CSV file after applying Group By & Sorting as provided in below PowerShell Script.

$dt=$ListItems.GetDataTable()
$go=$dt | Group-Object "ProjectName"
$ad=$go | Sort-Object -Property {$_.Values[0]},{$_.Count} -Descending
$ad | Select-Object -Property {$_.Values[0]},{$_.Count} | Export-CSV "D:\ProjectInfo.csv" -NoTypeInformation

The above code generating an CSV file where "ProjectName & Count" field in output CSV is not in Descending Order as mentioned below. Project10-8 Project11-12 Instead, output CSV file should generate "ProjectName & Count" field as shown below Project11-12 Project10-8 Please help me how to export CSV file with an Descending order | | | -------- |

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. AllenXu-MSFT 24,941 Reputation points Moderator
    2023-05-04T03:00:18.9866667+00:00

    Hi @Whizkidgps,

    Change $go | Sort-Object -Property {$_.Values[0]},{$_.Count} -Descending to $go | Sort-Object -Property Count -Descending in your code and the script will work perfectly.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.