Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#Requires -Version 3.0
Function ConvertTo-NicelyFormattedString {
[CmdletBinding()]
Param(
[Parameter(Mandatory,ValueFromPipeline)]
[string[]]$InputObject
)
Begin {}
Process {
$results = @()
$InputObject | ForEach-Object {
$_ -split '\s{1,}' | ForEach-Object {
$allLetters = $_.ToCharArray()
$FirstLetter = ($allLetters[0]).ToString().ToUpper()
if ($allLetters.Count -gt 1) {
$RemainingLetters = (-join ($allLetters[1.. ($allLetters.Count-1)])).toLower()
} else {
$RemainingLetters = [string]::Empty
}
$results += '{0}{1}' -f $FirstLetter,$RemainingLetters
}
}
$results -join ' '
}
End {}
<#
.SYNOPSIS
First letter to upper case and remaining to lower case
.DESCRIPTION
It will capitalize the first letter of each word in a string of text and convert the remaining letters in each word to lower case.
If the word is a single character, it will be converted to upper case.
.PARAMETER InputObject
InputObject is an array of string and each string can have more than one word.
.EXAMPLE
ConvertTo-NicelyFormattedString 'ThE wINDOWS PowerShell TFM book CONTEST aND GiVeAwAy'
.EXAMPLE
'ThE wINDOWS PowerShell TFM is a b1ook CONTEST aND GiVeAwAy',
'ThE wINDOWS PowerShell TFM iS A b2ook CONTEST aND GiVeAwAy',
'ThE wINDOWS PowerShell TFM is a b3ook CONTEST aND GiVeAwAy' | ConvertTo-NicelyFormattedString
.LINK
http://mikefrobbins.com/2015/09/23/windows-powershell-tfm-book-contest-and-giveaway/
#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment