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".
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.
I have a one-liner for this:
$t=”gone with the wind”
(New-Object System.Globalization.CultureInfo -ArgumentList @(‘en-US’,$false)).TextInfo.ToTitleCase($t)
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.
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.
😉