Link to home
Start Free TrialLog in
Avatar of Nick Collins
Nick Collins

asked on

Powershell Script - Extracting data from a csv file


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
Avatar of Nick Collins
Nick Collins


Avatar of oBdA

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial