Skip to content

Instantly share code, notes, and snippets.

/ConvertTo-Propercase.ps1 Secret

Created Oct 6, 2015
Embed
What would you like to do?
<#
.Synopsis
Function to captialize the first letter of each word in a string.
.EXAMPLE
'ThE wINDOWS Powershell TFM book CONTEST aNd GiVeAwAy' | ConvertTo-Propercase
The Windows Powershell Tfm Book Contest And Giveaway
#>
Function ConvertTo-Propercase
{
[OutputType([string])]
Param(
#A string value must be passed on the pipeline to this function. (Get-Help ConvertTo-Propercase -examples)
[Parameter(Mandatory=$True,
ValueFromPipeline=$True)]
$Input
)
# Splits the string passed to the function on whitespace values, and removes extra whitespace. #
[array]$Words = $Input.Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)
# Sets a counter for the number of words passed through the 'ForEach' loop. #
[int]$Counter = 0
[array]$Words | ForEach {
[int]$Counter++ | Out-Null
<# Retreives the string length for the current word and removes '1'
to compensate for the following 'substring' transformations which count from 0. #>
[int]$Length = $_.Length -1
<# Creates a '$Result' string and reconstructs the current word adjusting the characters as required.
Adds a whitespace character after the current word when it isn't the last word in the string. #>
[string]$Result += ($_.Substring(0,1).ToUpper()) + ($_.Substring(1,[int]$Length).ToLower())
IF([int]$Counter -lt [int]$Words.Count) {[string]$Result += [char]32}
}
Return $Result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment