How to Remove the First Character from a String in PowerShell?

While working on some PowerShell string manipulations, I tried removing the first character from a string. In this tutorial, I will explain how to remove the first character from a string in PowerShell using different methods with examples.

To remove the first character from a string in PowerShell using the Substring method, you simply call the method on your string variable and specify the starting index as 1. For example, if you have $string = "ExampleString", you can use $newString = $string.Substring(1) to create a new string without the first character.

Remove the First Character from a String in PowerShell

You might need to remove the first character from a string in various scenarios. PowerShell offers multiple ways to remove the first character from a string. Let me show you each method with examples.

Using the Substring() Method

The Substring() method is the best way to remove the first character from a PowerShell string. The Substring method extracts a portion of a string, starting from a specified position.

Syntax:

$string.Substring(startIndex, length)

Example:

Here is an example.

$string = "ExampleString"
$newString = $string.Substring(1)
Write-Output $newString  # Outputs: xampleString

In this example, $string.Substring(1) starts from the second character (index 1) and goes to the end of the string.

Here is the exact output in the screenshot below:

Remove the First Character from a String in PowerShell

Check out Replace Multiple Strings in a File Using PowerShell

Using the Remove() Method

The Remove method can also be used to remove the first character from a string in PowerShell. This method deletes a specified number of characters starting from a specified position.

Syntax:

Here is the syntax:

$string.Remove(startIndex, count)

Example:

Now, let me show you an example.

$string = "ExampleString"
$newString = $string.Remove(0, 1)
Write-Output $newString  # Outputs: xampleString

Here, $string.Remove(0, 1) removes one character starting from the first position (index 0).

Here is the output you can see in the screenshot below:

PowerShell Remove the First Character from a String

Using the -replace Operator

The -replace operator in PowerShell uses regular expressions to replace text. You can use it to remove the first character by replacing it with an empty string.

Syntax:

Here is the syntax:

$string -replace "pattern", "replacement"

Example:

Let me show you an example.

$string = "ExampleString"
$newString = $string -replace "^.", ""
Write-Output $newString  # Outputs: xampleString

The pattern ^. matches the first character, and replacing it with "" effectively removes it.

Here is the output you can see in the screenshot below:

How to Remove the First Character from a String in PowerShell

Read Convert a String to a Binary Number in PowerShell

Using Array Indexing

Strings in PowerShell can be treated as arrays of characters. By using array indexing, you can create a new string that excludes the first character.

Example:

Here is an example.

$string = "ExampleString"
$newString = $string[1..($string.Length - 1)] -join ""
Write-Output $newString  # Outputs: xampleString

In this example, $string[1..($string.Length - 1)] creates an array of characters from the second character to the end, and -join "" combines them back into a string.

You can see the output in the screenshot below:

Remove the First Character from a String in PowerShell Examples

Conclusion

In this tutorial, I explained how to remove the first character from a string in PowerShell using multiple ways, such as using the Substring method, the Remove method, the -replace operator, or array indexing, etc.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.