# ValidatePattern Parameter Attribute

**URL:** https://forums.powershell.org/t/validatepattern-parameter-attribute/4536
**Category:** PowerShell Help
**Created:** [July 19, 2015, 11:37pm UTC](https://forums.powershell.org/t/validatepattern-parameter-attribute/4536 "2015-07-19T23:37:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![marcoolinho-pappas](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@marcoolinho-pappas](https://forums.powershell.org/u/marcoolinho-pappas)
#### Post date: [July 19, 2015, 11:37pm UTC](https://forums.powershell.org/t/validatepattern-parameter-attribute/4536/1 "2015-07-19T23:37:43Z")

</div>

Hi.

Would you be so kind and assist me?

I want to specify two possible patterns for my parameter $ComputerName in my Advanced Script. How do I write two regex-s? How do I separate them and enclose them with quotes and brackets?

Thank you very much for your help in advance.

---

<div class="post-metadata">

### Author: ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)
#### Post date: [July 19, 2015, 11:47pm UTC](https://forums.powershell.org/t/validatepattern-parameter-attribute/4536/2 "2015-07-19T23:47:36Z")

</div>

Not sure that ValidatePattern could be specified twice for one parameter or that you could have it check against multiple patterns. It would probably be better to have a single pattern match. What are the possible computer names? Do you have any regex patterns built?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [July 20, 2015, 12:18am UTC](https://forums.powershell.org/t/validatepattern-parameter-attribute/4536/3 "2015-07-20T00:18:54Z")

</div>

Regex has an “or” operator (the pipe symbol) that can be used to combine two patterns into a single regex. However, the ValidatePattern attribute produces error messages that are extremely unfriendly to users, and its use is generally discouraged. Using ValidateScript, you can get the same functionality, but also produce any error messages you like via the “throw” statement.

---

<div class="post-metadata">

### Author: ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)
#### Post date: [July 20, 2015, 12:36am UTC](https://forums.powershell.org/t/validatepattern-parameter-attribute/4536/4 "2015-07-20T00:36:13Z")

</div>

@Dave - Here is another option I’d seen before which uses a Regex comment to give a descriptive error message:

[http://powershell.com/cs/blogs/tips/archive/2013/07/19/using-validatepattern-attribute.aspx](http://powershell.com/cs/blogs/tips/archive/2013/07/19/using-validatepattern-attribute.aspx)

---

<div class="post-metadata">

### Author: ![marcoolinho-pappas](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@marcoolinho-pappas](https://forums.powershell.org/u/marcoolinho-pappas)
#### Post date: [July 27, 2015, 12:25am UTC](https://forums.powershell.org/t/validatepattern-parameter-attribute/4536/5 "2015-07-27T00:25:01Z")

</div>

Thank you everyone for your assistance. Please excuse my late response.  
I used the “|” pipe sign to combine two Regex-s because I needed it for my function which creates Gen2 VMs on my Hyper-V server and I wanted them to be according to my naming standards: for virtual servers i.e. “VM-Server-DC01” and for virtual workstations i.e. “W81Ent01”.

[ValidatePattern(“^VM-Server-([A-Z]{2,3})([0-9]{1,2})$|^W81([A-Z]{3})([0-9]{1,2})$”)]

What do you think?

---

<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:45pm UTC](https://forums.powershell.org/t/validatepattern-parameter-attribute/4536/6 "2024-05-16T20:45:00Z")

</div>


