简体   繁体   中英

Powershell creating an array from a csv of column A based on contents of column B

I have a csv file that contains a host of columns but only 2 are relevant: "Emails" and "Followup". The Emails column contains a list of email addresses, and Followup contains either Y's or N's; I have 1 Y and 7 N's. I'm working with a header row and then 8 rows of data.

So far, I have this script:

$File = Import-Csv -Path "C:\file.csv"
$Addresses = @($File.email)
$Followup = @($File.followup)
$To = @($Addresses | Where-Object {$Followup -eq "Y"})

$Addresses
$Followup
$To

I'm trying to create another array ($To) that contains only those email addresses that are paired with a "Y" in the Followup column.

For my output, I'm getting 8 email addresses (good), 7 N's and 1 Y (good), and 8 email addresses again (bad, desired outcome is 1).

What am I fouling up here?

Instead of splitting the email and followup columns into separate arrays, keep them together, so that Where-Object can operate on the followup property and output the object with the corresponding email :

$File = Import-Csv -Path "C:\file.csv"

# Filter on the `followup` column
$ShouldFollowup = $File |Where-Object followup -eq 'Y'

# Grab ONLY the value of the `email` column
$Addresses = $ShouldFollowup.email

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM