简体   繁体   中英

Powershell check email address against a list of email addresses

I've been stuck for too long on this task. Im trying to check if a variable string($email) is in a list of strings($emaillist).

so if

$email = "you@email.com"

$t = $emaillist | Select-String $email

#If statement#$
if($t) 
{
write-host "yes" -ForegroundColor Green
}
else
{
write-host "No" -ForegroundColor red
}

This doesn't work because partials of the string passes. So I think I need to use some regex? '^$' Not sure how to use regex and the variable?

Any help is appreciated Thanks

If $emaillist is indeed a list of strings, why use Select-String ??

Just do

if ($emaillist -contains $email) {
    write-host "yes" -ForegroundColor Green
}
else {
    write-host "No" -ForegroundColor red
}
if ($emaillist -contains $email){
write-host "yes" -ForegroundColor Green
}

you could also use -in

some examples

$emaillist = @('abc@gmail.com','edf@gmail.com')
$email = 'abc@gmail.com'

$emaillist -contains $email ## true

$email -in $emaillist ##  returns true

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