Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Don't you need to Write-Host or something as well? Or was that code just omitted? –  RJ Cuthbertson Sep 21 '12 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 '12 at 16:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.