Powershell Irish curse

Hi all, this may be a simple one to answer but it's eluding me at the moment. I have quite a few users on a network I have inherited with surnames like O'Brian and O'Sullivan. This is making the use of simple arrays a nightmare. So put simply if I have an array like this:
$Array = @('JoeBrown',
'TomO'Brian',
'MattO'Sullican'
'SallySmith'
)

How can I escape the Apostrophe? I have tried numerous combinations of quotes, double quotes and backticks all to no avail
Dead_EyesAsked:
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.

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
You can use single quotes inside of double quotes.

$Array = @("JoeBrown",
"TomO'Brian",
"MattO'Sullican"
"SallySmith"
)

Open in new window

0
Vaseem MohammedCommented:
This should work
$Array = @(
"TomO'Brian",
"MattO'Sullican"
)
0

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
oBdACommented:
It's obviously easier to use double quotes, since escaping the occasional $ is easier than escaping a lot of single quotes, but for the record, a single quote inside single quotes needs to be escaped by doubling up on it:
$Array = @(
	'JoeBrown',
	'TomO''Brian',
	'MattO''Sullican',
	'SallySmith'
)

Open in new window

1
Dead_EyesAuthor Commented:
Thanks guys and doh!, think when I tried that before I must have had multiple Apostrophes to close together. That or the usual IT sleep deprivation lol. Anyways thanks that's made the rest of the day a lot easier.
0
oBdACommented:
Btw: another way to turn strings into arrays would be to use a here-string and split it; saves even more quotes and commas (note that the closing quote of the here-string may not be indented):
$Array = @"
	JoeBrown
	TomO'Brian
	MattO'Sullican
	SallySmith
"@.Split("`r`n`t", [StringSplitOptions]::RemoveEmptyEntries)

Open in new window

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.