2

I am looking for a way to get list values from SharePoint 2007. Right now I am using PowerShell, and have the following script:

#load Microsoft.SharePoint.dll
[System.Reflection.Assembly]::LoadFrom("$12HivesDir\ISAPI\Microsoft.SharePoint.dll")
#returns the SPSite object from the specified URL
function get-spweb ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
   $site =  New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
   return $site.OpenWeb();
}

$web = get-spweb http://myURL
$myList = $web.Lists["ListName"]

foreach($item in $myList.Items)
{
    $item.Title
    foreach($field in $item.Fields)
    {
        $field.Title
    }
}

This script prints out the column headers, but I need to get the actual values. What is the best way to do it?

Thanks.

1 Answer 1

1

Try this:

foreach($item in $myList.Items)
{
    $item.Title
    foreach($field in $item.Fields)
    {
        $item[$field.Title]
    }
}

This uses the title as the index of the list item in a dynamic manner. Hope this helps.

2
  • Don't you need to Write-Host or something as well? Or was that code just omitted? Sep 21, 2012 at 18:28
  • Thanks for the help rjcup. Write-Host is not necessary. The way the script is written it will still output the values in Powershell. Also, without Write-Host, I am able to use output redirection to a file.
    – laconicdev
    Sep 22, 2012 at 16:38

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.