Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF exists ELSE re: values from csv

I have a csv input to a PowerShell script that has two email fields; one that will always contain a value and one that sometimes will, and when the latter value exists, that's the one I want to use to set a value used to set a mailcontact field later in the script, for the variable $WriteEmail.

So I've got the following to read the CSV:

# Read contacts
ForEach ($contact in $contactfile) {

# Read attributes
$sourceEmail=$contact.Email
$sourceOutlookEmail=$contact.OutlookEmail

And I need it to basically do:

IF $sourceOutlookEmail IsNullOrWhitespace
THEN $WriteEmail=$sourceEmail
ELSE $WriteEmail=$sourceOutlookEmail

I just can't get the syntax correct, I've tried all kinds of stuff. I know it's relatively simple, I just can't get it to work.

like image 588
km457 Avatar asked Feb 25 '26 20:02

km457


1 Answers

You can use the static .net IsNullOrWhiteSpace method:

if ([string]::IsNullOrWhiteSpace($sourceOutlookEmail))
{
   $WriteEmail=$sourceEmail
}
else
{
   $WriteEmail=$sourceOutlookEmail
}
like image 191
Martin Brandl Avatar answered Feb 27 '26 16:02

Martin Brandl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!