Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to format words as UpppercaseLowerCase
Function Format-WordCase {
<#
.Synopsis
Formats a string as uppercase first letter of each word
.DESCRIPTION
The function will take any string and parses each word in order to apply an uppercase first letter and then lowercase every other letter of the word.
.EXAMPLE
Format-WordCase -String "MY STRING has NO SPEcfic case HeRe"
This cmdlet will output "My String Has No Specfic Case Here".
.Example
"MY STRING has NO SPEcfic case HeRe", "we are the champions" | Format-WordCase
Formatting words by piping directly an array.
.INPUTS
System.String
.OUTPUTS
System.String
.NOTES
Quick function for Mike F. Robbins contest
.LINK
http://mikefrobbins.com/2015/09/23/windows-powershell-tfm-book-contest-and-giveaway/
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[string[]]$String
)
Process{
$String -split " " | ForEach-Object{
$Case = $_.SubString(0,1).ToUpper()
$Case += $_.Substring(1).ToLower() + " "
Write-Verbose -Message "Modified `"$_`" to `"$Case`""
$Case
} | ForEach-Object{
$Final += $Case
Write-Verbose -Message "$Final - $Case"
}
$Final
Remove-Variable Case,Final
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment