Camel case is a popular programming naming convention in which the first letter of the first word is lowercase, and the first letter of each subsequent word is capitalized, with no spaces or punctuation between words. For example, “convert string to camel case” becomes “convertStringToCamelCase.” This naming convention is often used in variable and function names. In this PowerShell tutorial, I will show you different methods to convert a string to camel case in PowerShell.
To convert a string to camel case in PowerShell, you can use a function that splits the string into words, capitalizes the first letter of each word except the first one, and then joins them back together. For example, the function ConvertTo-CamelCase takes an input string, converts it to lowercase, splits it into words, capitalizes the necessary letters, and joins them to form a camel case string like “convertStringToCamelCase”.
Method 1: Using Regular Expressions
Regular expressions (regex) are powerful for string manipulation. In PowerShell, you can use the -replace operator with regex to achieve camel case conversion.
Example
Here is an example of converting string to Camel case in PowerShell using regular expressions.
function ConvertTo-CamelCase {
param (
[string]$inputString
)
# Convert the string to lowercase and split it into words
$words = $inputString.ToLower() -split '\s+'
# Capitalize the first letter of each word except the first one
for ($i = 1; $i -lt $words.Length; $i++) {
$words[$i] = -join ([string]$words[$i][0].ToUpper(), $words[$i].Substring(1))
}
# Join the words together without spaces
$camelCaseString = -join $words
return $camelCaseString
}
# Example usage
$input = "convert string to camel case"
$camelCase = ConvertTo-CamelCase -inputString $input
Write-Output $camelCase # Output: convertStringToCamelCaseExplanation
- Parameter Definition: The function
ConvertTo-CamelCasetakes a single string parameter$inputString. - Lowercase Conversion and Splitting: The input string is converted to lowercase and split into an array of words using the
-splitoperator with a regex pattern that matches any whitespace (\s+). - Capitalization: A
forloop iterates through the array starting from the second word (index 1). For each word, the first character is converted to uppercase and concatenated with the rest of the word. - Joining Words: The
-joinoperator concatenates the array of words back into a single string. - Output: The resulting camel case string is returned.
Check out How to Split String by Tab Character in PowerShell?
Method 2: Using String Manipulation
Let me show you now how to use string manipulation to convert a string to Camel case in PowerShell without relying heavily on regex.
Below is an example of how to do it.
Example
function ConvertTo-CamelCaseSimple {
param (
[string]$inputString
)
# Split the string into words
$words = $inputString.Split(" ")
# Capitalize the first letter of each word except the first one
for ($i = 1; $i -lt $words.Length; $i++) {
$words[$i] = $words[$i].Substring(0, 1).ToUpper() + $words[$i].Substring(1).ToLower()
}
# Join the words together
$camelCaseString = $words[0].ToLower() + ($words[1..$words.Length] -join "")
return $camelCaseString
}
# Example usage
$input = "convert string to camel case"
$camelCase = ConvertTo-CamelCaseSimple -inputString $input
Write-Output $camelCase # Output: convertStringToCamelCaseExplanation
- Parameter Definition: The function
ConvertTo-CamelCaseSimpletakes a single string parameter$inputString. - Splitting: The input string is split into an array of words using the
Splitmethod with a space as the delimiter. - Capitalization: A for loop iterates through the array starting from the second word (index 1). The first character is converted to uppercase for each word, and the rest is converted to lowercase.
- Joining Words: The first word is converted to lowercase, and the rest of the words are concatenated using the
-joinoperator. - Output: The resulting camel case string is returned.
I executed the above PowerShell code, and you can see the output in the screenshot below:

Check out PowerShell String Replace
Method 3: Using the TextInfo Class
You can also use the TextInfo class, which is a .NET class in PowerShell, for more advanced string manipulations.
Example
Here is an example to help you understand it better.
function ConvertTo-CamelCaseWithTextInfo {
param (
[string]$inputString
)
# Create a TextInfo object
$textInfo = [System.Globalization.CultureInfo]::CurrentCulture.TextInfo
# Convert the string to title case
$titleCaseString = $textInfo.ToTitleCase($inputString.ToLower())
# Remove spaces and convert the first character to lowercase
$camelCaseString = $titleCaseString -replace ' ', ''
$camelCaseString = $camelCaseString.Substring(0, 1).ToLower() + $camelCaseString.Substring(1)
return $camelCaseString
}
# Example usage
$input = "convert string to camel case"
$camelCase = ConvertTo-CamelCaseWithTextInfo -inputString $input
Write-Output $camelCase # Output: convertStringToCamelCaseExplanation
- Parameter Definition: The function
ConvertTo-CamelCaseWithTextInfotakes a single string parameter$inputString. - TextInfo Object: A
TextInfoobject is created using the current culture’s text information. - Title Case Conversion: The input string is converted to title case using the
ToTitleCasemethod, which capitalizes the first letter of each word. - Removing Spaces: The spaces are removed using the
-replaceoperator. - Lowercase First Character: The first character of the resulting string is converted to lowercase.
- Output: The resulting camel case string is returned.
I executed the above PowerShell script, and you can see the output in the screenshot below:

Conclusion
In this tutorial, I explained how to convert a string to camel case in PowerShell using several methods, such as regular expressions, simple string manipulation, or leveraging .NET classes.
You may also like the following tutorials:
- How to Convert String to Double in PowerShell?
- Convert String to Int in PowerShell
- Convert Strings to Lowercase or Uppercase in PowerShell
- Convert Multiline String to Single Line in PowerShell
- PowerShell: Uppercase the First Letter of Each Word
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.