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.