• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 100
  • Last Modified:

powershell - title case but also deal with apostrophes

I am changing to title case names.
the command below works Ok

$name=blah
(Get-Culture).textinfo.totitlecase($name)


this gives
mary to Mary
mary jane to Mary Jane
mary-jane to Mary-Jane

but cant seem to deal with apostophes
o'reilly to O'reilly

I need a capital R as well as O

any ideas?
Thanks
0
Pete
Asked:
Pete
2 Solutions
 
oBdACommented:
The brute force approach maybe (you can use most non-alphanumeric characters instead of the underscore):
$Name = "o'reilly"
(Get-Culture).textinfo.totitlecase($Name.Replace("'", "_")).Replace("_", "'")

Open in new window

0
 
Bill PrewCommented:
Maybe someone will come forward with some clever code they have written, but in general formatting names can be a bit of a challenge.  As you have seen apostrophes are not handled by ToTitleCase.  To solve that you would likely want to follow it's use by a replace with a regular expression that looks for an apostrophe followed by a letter and capitalizes the letter.

Keep in mind though there are a lot of other situations that pop up in names, and you probably can't get them all right.  For example "Vincent van Gogh" where the second word should not be capitalized.  Or "McBride" where prefies like Mc and Mac often need to be followed by a capital letter.

Also keep in mind that ToTitleCase does nothing on input strings that are all caps, so if you are using on names it's best to convert to all lower case before passing to ToTitleCase.

~bp
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now