-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConvertTo-NameCase.ps1
36 lines (32 loc) · 1.25 KB
/
ConvertTo-NameCase.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function ConvertTo-NameCase ([string[]]$Names) {
<#
.SYNOPSIS
ConvertTo-NameCase takes any english name variation and converts it to the correct case format
.DESCRIPTION
ConvertTo-NameCase takes any english name variation and converts it to the correct case, it will handle upper, lower, hyphens, and apostrophe
.PARAMETER Name
Name The name you wish to correct in the form of a string
.EXAMPLE
ConvertTo-NameCase -Names "kevin"
#Output = Kevin
ConvertTo-NameCase -Names "kevin" "o'leary"
#Output = Kevin
#O'Leary
.NOTES
General notes
#>
foreach ($Name in $Names) {
$NameArray = $Name.ToCharArray() #Get a Character array, basically a split
#Loop through the array and do the correct case
for ($i = 0; $i -lt $Name.Length; $i++) {
$NameArray[0] = ([string]$Name[0]).ToUpper()
if ($NameArray[$i] -eq '-' -or $NameArray[$i] -eq "'") {
$NameArray[$i + 1] = ([string]$NameArray[$i + 1]).ToUpper()
$i++ #Tell the loop to skip the next itteration
} else {
$NameArray[$i] = ([string]$Name[$i]).ToLower()
}
}
$NameArray -join '' #Join the Character Array back into a string
}
}