Converting a string to lower case

PowerShell:
Converting a string to lower case

How to:

PowerShell is pretty handy with strings. Use the .ToLower() method, like this:

$string = "HELLO, World!"
$lowerCaseString = $string.ToLower()
$lowerCaseString

Output:

hello, world!

Or try the ToLowerInvariant() method when cultural norms shouldn’t affect the conversion:

$string = "HELLO, World!"
$lowerCaseInvariant = $string.ToLowerInvariant()
$lowerCaseInvariant

Output:

hello, world!

Deep Dive

Once upon a time, case insensitivity was pretty common in programming languages. In PowerShell, like its .NET ancestors, strings are objects with built-in methods for manipulation. When we use .ToLower(), we’re invoking a method that handles the conversion process for us.

Alternative ways to get the job done? Sure. You could use:

  • a for loop, visiting each character, and case switching manually
  • Regular Expressions with the -replace operator
  • Culture-specific conversions using overloads of .ToLower()

Why use the invariant culture with ToLowerInvariant()? It’s essential for consistent results across different locales where the interpretation of what is a “lower” case could differ.

See Also

For more detailed adventures in string manipulation, visit these links: