Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Convert to Title Case

Posted on January 10, 2011

After a long holiday break, some travel and a few training classes its time to get back in the swing of things. Today I have a relatively simple function, that if nothing else demonstrates how to use object methods. The challenge is to take a string of text and convert it into title case; so that "great expectations" becomes "Great Expectations".

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

To accomplish this, all I need to do is get the first letter of each string and invoke the string object's ToUpper() method. But before I can do that I need to break the title string into an array of individual words which is easily accomplished using the Split() method.
[cc lang="PowerShell"]
PS S:\> $t="gone with the wind"
PS S:\> foreach ($w in $t.split()) {$w}
gone
with
the
wind
[/cc]
Now I have to convert the first letter of each word to upper case and then append the rest of the word. I'll use the SubString() method.
[cc lang="PowerShell"]
PS S:\> foreach ($w in $t.split()) {$w.substring(0,1).ToUpper()+$w.Substring(1)}
Gone
With
The
Wind
[/cc]
I don't really like using concatenation but it works here but I still have an array. I need to tell PowerShell to treat the entire array as a single string.
[cc lang="PowerShell"]
PS S:\> [string]$title=foreach ($w in $t.split()) {$w.substring(0,1).ToUpper()+$w.Substring(1)}
PS S:\> $title
Gone With The Wind
[/cc]
The actual conversion only takes a single line of PowerShell, but I went ahead and created an advanced function called ConvertTo-TitleCase to make it easier to use.
[cc lang="PowerShell"]
Function ConvertTo-TitleCase {

<# .Synopsis Convert a string to title case. .Description Take a string and return a version in title case where each word is capitalized. .Parameter Text The string you wish to convert to title case. .Example PS C:\> ConvertTo-TitleCase "a tale of two cities"
A Tale Of Two Cities

.Notes
NAME: ConvertTo-TitleCase-
VERSION: 1.0
AUTHOR: Jeffery Hicks
LASTEDIT: 1/10/2011

Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010)

.Link
Http://jdhitsolutions.com/blog

.Link
Out-String

.Inputs
[string]
.Outputs
[string]

#>

[cmdletBinding()]

Param(
[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True)]
[ValidateNotNullOrEmpty()]
[string[]]$Text

)

Begin
{
Write-Verbose -Message "Starting $($myinvocation.mycommand)"

} #close Begin

Process {
ForEach ($string in $Text)
{
Write-Verbose -Message "Processing $string"

#split the string into indvidual words
$tmp=$string.Split()

#define an array to hold modified results
$out=@()
foreach ($word in $tmp)
{
#take the first letter of each word and make it upper case,
#then append the rest of the word
$out+="{0}{1}" -f $word.SubString(0,1).ToUpper(),$word.substring(1)
}

#write the results
Write-Output ($out -as [string])
}
} #close process

End
{
Write-Verbose -Message "Ending $($myinvocation.mycommand)"
} #close End

} #end Function
[/cc]
The function can be run like this:
[cc lang="PowerShell"]
PS S:\> ConvertTo-TitleCase "the wizard of oz"
The Wizard Of Oz
[/cc]
Or it can accept pipelined input.
[cc lang="PowerShell"]
PS S:\> "the shining","the stand","under the dome","the dark tower" | ConvertTo-TitleCase
The Shining
The Stand
Under The Dome
The Dark Tower
[/cc]
Download the script file ConvertTo-TitleCase.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

4 thoughts on “Convert to Title Case”

  1. Pingback: Tweets that mention Convert to Title Case | The Lonely Administrator -- Topsy.com
  2. Tome Tanasovski says:
    January 10, 2011 at 10:13 am

    I have a one-liner for this:
    $t=”gone with the wind”
    (New-Object System.Globalization.CultureInfo -ArgumentList @(‘en-US’,$false)).TextInfo.ToTitleCase($t)

    1. Jeffery Hicks says:
      January 10, 2011 at 10:49 am

      One liners like that are very slick. I can’t argue with the results. But one liners like this often assume an in-depth knowledge of the .NET framework which means they are not easy to discover or figure out. My approach my seem a little more involved in terms of the number of steps, but I think it is more meaningful to a typical PowerShell administrator. Personally I think we should only dive into “raw” .NET classes and methods when there is no acceptable PowerShell option.

  3. Jim says:
    January 10, 2011 at 1:32 pm

    Lest we forget the old libraries:

    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.VisualBasic”)
    [Microsoft.VisualBasic.Strings]::StrConv(“Hello World”,”ProperCase”,0)

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.aspx

    The ‘Strings’ class has many usefule extensions not available in the rest of the base Framework.

    😉

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d