Powershell Regular Expressions

Hi All,

I have this line of code in my powershell script:

#Only for Marie Curie users
if ($user.email -like "*@asda.com") {

     
    $GetADUser = {Get-ADUser -Filter "EmailAddress -eq '$($user.email)'" -Properties * -ErrorAction Stop}

.....................
........
..........
}

problem is that if there is an comma in the email, e.g testacco'unt1@asda.com, the script fails, how can i validate the script so there isn't a comma?
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SunilTechnical Specialist - Wintel Operations.Commented:
Do you mean , (comma) or ' (quote)?
SunilTechnical Specialist - Wintel Operations.Commented:
Refer this link to use the escape character in the powershell
http://www.rlmueller.net/PowerShellEscape.htm
footechCommented:
A quick note - nothing in the code is making any use of regular expressions.  "-eq" does a direct match, and "-like" enables the use of the wildcard "*".
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Kelly GarciaSenior Systems AdministratorAuthor Commented:
apologies i meant quote
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You'll indeed have to escape single quotes here, as you are building a string containing a string expression. It is probably much easier to build the filter string differently:
    $GetADUser = {Get-ADUser -Filter "EmailAddress -eq '" + $user.email.Replace("'", "''") + "'" -Properties * -ErrorAction Stop}

Open in new window

or use a scriptblock:
    $GetADUser = {Get-ADUser -Filter {EmailAddress -eq "$($user.email)"} -Properties * -ErrorAction Stop}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sajen JoseCommented:
You can probably try this regex

If ($user.email -match "^[a-zA-Z0-9.!£#$%&'^_`{}~-]+@asda.com")

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Sajen, how should that help? The AD query still would have the quote. and the original IF condition is sufficient to catch email addresses for that domain (which is the intent).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.