0

I'm trying to get a sharepoint list choice field value using powershell. The choice field contains a yes and no values. now I want to check if it's a Yes and do some logic.

foreach($item in $listItems) 
{     
               $value= $list.items | where {$item["Exported)"] -eq $item["Exported)"]."Yes"
               if($value)
               {
                $ExportItem = New-Object PSObject 
                $ExportItem | Add-Member -MemberType NoteProperty -name "Date" -value $item["Date"]
                $ExportItem | Add-Member -MemberType NoteProperty -Name "Employer" -value $item["Employer"]
                $ExportItem | Add-Member -MemberType NoteProperty -name "Employer Number" -value $item["Employer Number"]
                $ExportItem | Add-Member -MemberType NoteProperty -Name "Classification" -value $item["Classification"]
                $ExportItem | Add-Member -MemberType NoteProperty -name "Source" -value $item["Source"]
                $ExportItem | Add-Member -MemberType NoteProperty -Name "Action" -value $item["Action"]
                $ExportItem | Add-Member -MemberType NoteProperty -name "Description" -value $item["Description"]
                $ExportItem | Add-Member -MemberType NoteProperty -Name "Exported" -value $item["Exported"]

                $ListItemCollection += $ExportItem
                $ListItemCollection | Export-CSV "C:\Users\Documents\CSV uotput\Test.CSV" -NoTypeInformation
               }
}

this exports the data regardless whether the choice field is a "No", what I want to export items that have the choice field as "Yes"

Please assist.

1 Answer 1

1

Multiple things that seems of with your code. "Exported" has a ) in it's name. Followed by . + the string to check against.

You need to declare $ListItemCollection as an array to be able to push multiple values to it. But I think you already have but it's not in the part of your script that you provided.

It's always good to set the parameters delimiter and encoding for your CSV, if you wanna be able to work further with your data.

$web = Get-SPWeb "WebNameGoesHere"

$list = $web.Lists["ListNameGoesHere"]

$ListItemCollection = @()

foreach($item in $list.Items) {  

    if($item["Exported"] -eq "Yes") {

        $ExportItem = New-Object -TypeName PSObject
        $ExportItem | Add-Member -MemberType NoteProperty -Name "Date" -value $item["Date"]
        $ExportItem | Add-Member -MemberType NoteProperty -Name "Employeer" -value $item["Employer"]
        $ExportItem | Add-Member -MemberType NoteProperty -Name "Employee Number" -value $item["Employer Number"]
        $ExportItem | Add-Member -MemberType NoteProperty -Name "Classification" -value $item["Classification"]
        $ExportItem | Add-Member -MemberType NoteProperty -Name "Source" -value $item["Source"]
        $ExportItem | Add-Member -MemberType NoteProperty -Name "Action" -value $item["Action"]
        $ExportItem | Add-Member -MemberType NoteProperty -Name "Description" -value $item["Description"]
        $ExportItem | Add-Member -MemberType NoteProperty -Name "Exported" -value $item["Exported"]

        $ListItemCollection += $ExportItem
    }
}
$ListItemCollection | Export-CSV -Path "C:\Users\Documents\CSV output\Test.CSV" -Encoding UTF8 -Delimiter ";" -NoTypeInformation

I would also recommend to use test-path before exporting data to a CSV, to make sure that the path actually exists and create it if it don't.

$exportPath = "C:\Users\Documents\CSV output"

If(!(test-path $exportPath ))
{
    New-Item -ItemType Directory -Path $exportPath | Out-Null
}

Export-CSV -Path "$exportPath \Test.CSV"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.