Powershell script to export list view data to spreadsheet

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteUrl=”http://sharepoint/sites/co”
$ListName = “Pipeline”
$ViewName = “All Items”

#Get Web and List objects
$web = Get-SPWeb $SiteURL
$list = $web.Lists[$ListName]

#Get the View
$view = $list.Views[$ViewName]

#Get All Items from the View
$items = $list.GetItems($view)
$view.rowlimit = 100000

#This will temporarily update the view row limit – it gets set back to 30 at the end of the script

$view.update()
$vfields=$view.viewfields
$lfields=$list.fields
$efields=@()
foreach($vf in $vfields)
{
foreach($lf in $lfields)
{
if($lf.internalname -eq $vf)
{
$efields += $lf.title
}
}
}

$items = $list.getitems($view)
if($items.count -gt 0)
{
$exportlist = $null
$exportlist = @()
$items | foreach {
$hash = $null
$hash = @{}
foreach($ef in $efields)
{
$hash.add($ef, $_[$ef])
}
$obj = New-Object PSObject -Property $hash
$exportlist += $obj
}
$sel=@()
foreach($efield in $efields)
{
$sel+=$efield
}
$expath = ‘D:\ExportLV.csv’
$exportlist |select $sel | Export-Csv -path $expath -notypeinformation
$view.rowlimit = 30
$view.update()
}

$web.dispose()

Leave a comment