Powershell & Sharepoint export list items via view to csv

Jake picture Jake · Jan 25, 2011 · Viewed 12k times · Source

I am attempting to export items from a sharepoint list via a specific View.

I have the items returning successfully but now I am stuck trying to export it to a csv file correctly

here is what i have thus far:

$web = Get-SPWeb $url
    $splist = $web.Lists[$listname]
    $view = $splist.Views["Current Month"] 
    $items = $splist.GetItems($view)
    $items | Select-Object "Check Number", "Purchaser" | Export-Csv -Path f:\test.csv
    $web.Dispose()

Thank you in advance!

Answer

djeeg picture djeeg · Jan 26, 2011

For sharepoint 2007

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | out-null
$site = new-object Microsoft.SharePoint.SPSite("http://yoursite")
$web = $site.RootWeb
$list = $web.Lists[$listname]
$view = $list.Views["Current Month"] 
$items = $list.GetItems($view)
$items | %{ select-object -input $_ -prop @{Name='Title';expression={$_.Title;}}, @{Name='Check Number';expression={$_["Check Number"];}}; } | Export-Csv -Path c:\test.csv

You have to build your select object up from the SPListItem properties before passing it to Export-CSV