Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#Requires -Version 3.0
Function ConvertTo-NicelyFormattedString {
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 {}
First letter to upper case and remaining to lower case
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.
ConvertTo-NicelyFormattedString 'ThE wINDOWS PowerShell TFM book CONTEST aND GiVeAwAy'
'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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment