1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant. I hadn't considered nesting the arrays like that; got me just what I was looking for. Thanks for the assist!

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.