Powershell Script - Extracting data from a csv file

Nick Collins
Nick Collins used Ask the Experts™

I would like some help with a powershell script that extracts names from a CSV file based on particular criteria.

$TargetLabels = @("Select the operatives -")

$a = Import-Csv  D:\source.csv

$a | % -begin{$rec=@(); $o=@{}} `
       -process{ `
        if ($_.label -eq "End of Site Details") {
            if ($o.count -ne 0) {
                $rec += new-object -TypeName psobject -Property $o
    if ($TargetLabels -contains $_.label){
        $o += @{$_.label=$_.response}
#add the one we were constructing
$rec += new-object -TypeName psobject -Property $o
$rec | Export-Csv -Path D:\iauditor_exports_folder\DailyWorksheets\Output\DataNames.txt -NoTypeInformation

Open in new window

In the source file it is extracting the names in the row where 'Select the operatives -' appears

On initial testing it is extracting names on every row but it misses the row where the names 'Wayne Pepperpot, Graham Prickstick'

Any ideas?

Names in the source file are made up
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
That happens because in that row, the Label field ends with two Linefeed characters:
list,"Select the operatives -<LF><LF>","Wayne Pepperpot<LF>Graham Prickstick",

Open in new window

That could be handled in the script, but the question is whether this is actually supposed to happen - it should probably be fixed at the source that created the export.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial