# Param validate set email address

**URL:** https://forums.powershell.org/t/param-validate-set-email-address/10548
**Category:** PowerShell Help
**Created:** [April 19, 2018, 3:10pm UTC](https://forums.powershell.org/t/param-validate-set-email-address/10548 "2018-04-19T15:10:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)
#### Post date: [April 19, 2018, 3:10pm UTC](https://forums.powershell.org/t/param-validate-set-email-address/10548/1 "2018-04-19T15:10:52Z")

</div>

Hi,

I have a function which has a number of required params, 1 of which is for a email address.

I’m struggling to create a validate set in email format.

Im finding it hard cause i need to cover “@name.co.uk”, “@name.com”, “@name.org” etc. and any format for the first part.

So i need it to say “must be something@something.something”

And clues?

Thanks

Tommy

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 19, 2018, 3:20pm UTC](https://forums.powershell.org/t/param-validate-set-email-address/10548/2 "2018-04-19T15:20:30Z")

</div>

A lot of smart people already solved these kind of tasks long time ago. No need to re-invent the wheel again and again … 😉😃

[http://emailregex.com](http://emailregex.com)/

---

<div class="post-metadata">

### Author: ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)
#### Post date: [April 19, 2018, 6:05pm UTC](https://forums.powershell.org/t/param-validate-set-email-address/10548/3 "2018-04-19T18:05:51Z")

</div>

And with that, you can also use the more appropriate ValidatePattern:

```
param(
    [Parameter()]
    [ValidatePattern("regexstring")]
    [string]
    $EmailAddress
)
```

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 20, 2018, 12:37am UTC](https://forums.powershell.org/t/param-validate-set-email-address/10548/4 "2018-04-20T00:37:14Z")

</div>

… and with some more research you would find something like this what’s even cooler

```
function IsValidEmail {
param([string]$EmailAddress)
try {
$null = [mailaddress]$EmailAddress
return $true
}
catch {
return $false
}
}
```
  
It’s been found here:  
[https://stackoverflow.com/questions/48253743/powershell-to-validate-email-addresses?utm\_medium=organic&utm\_source=google\_rich\_qa&utm\_campaign=google\_rich\_qa](https://stackoverflow.com/questions/48253743/powershell-to-validate-email-addresses?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa)

---

<div class="post-metadata">

### Author: ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)
#### Post date: [April 20, 2018, 6:28am UTC](https://forums.powershell.org/t/param-validate-set-email-address/10548/5 "2018-04-20T06:28:49Z")

</div>

Oh, if there’s a data type designed for it, I’d just [ValidateNotNull()] and have that be the parameter type. No need to mess with complex validations at that point.

```
param(
    [Parameter()]
    [ValidateNotNull()]
    [mailaddress]
    $EmailAddress
)
```

Though, depending on your purposes there may be positives and negatives to either approach. 🙂

---

<div class="post-metadata">

### Author: ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)
#### Post date: [April 20, 2018, 3:20pm UTC](https://forums.powershell.org/t/param-validate-set-email-address/10548/6 "2018-04-20T15:20:43Z")

</div>

Thanks for this guys, amazing whats out on the net 😉

So anyway i’ve added the regex for validate pattern which works perfectly, but can how can i get the error message to say it needs to be in email format rather than

```
The argument “name” does not match the “^\w+([-+.']\w+)_@\w+([-.]\w+)_.\w+([-.]\w+)*$” pattern
```

Thanks

Tom

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:35pm UTC](https://forums.powershell.org/t/param-validate-set-email-address/10548/7 "2024-05-16T20:35:08Z")

</div>


