Search This Blog

Monday, March 5, 2018

SharePoint - Document List Item Export with CSV - PowerShell

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"

#Get the Web
$web = Get-SPWeb -identity "http://win-2016"

#Get the Target List
$list = $web.Lists["Documents"]

#Array to Hold Result - PSObjects
$ListItemCollection = @()

#CAML Query
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = 2000
$caml="<OrderBy><FieldRef Name='ID' Ascending='True' /></OrderBy>"
$spQuery.Query = $caml

#Get All List items
foreach($item in $listItems) {
$data = @{
       $ExportItem = New-Object PSObject -Property $data | Select "URL"
       $ExportItem | Add-Member -MemberType NoteProperty -name "Name" -value $item["Name"]     
       #Add the object with property to an Array
       $ListItemCollection += $ExportItem
#Export the result Array to CSV file
$ListItemCollection | Export-CSV "c:\ListData.csv" -NoTypeInformation                      

#Dispose the web Object