How to Split a String and Get the First and Last Element in PowerShell?

One of my clients has one requirement to get the last element of a string in PowerShell. I tried this using the PowerShell -split operator. This tutorial will guide you through how to split a string and get the first and last element in PowerShell, as well as how to remove the last element from a string. Let us check out a few examples.

Split a String in PowerShell

PowerShell provides several ways to split strings. The most common method is using the -split operator. The -split operator splits a string into substrings based on a delimiter, which can be a character or a string.

For example, let’s say we have a string like below:

$cityNames = "New York,Los Angeles,Chicago,Houston,Phoenix"

Using the -split operator, we can split this string into an array of city names:

$cityArray = $cityNames -split ","

Now, $cityArray contains the following elements:

New York
Los Angeles
Chicago
Houston
Phoenix

Split String and Get First Element in PowerShell

Here is an example of how to split a string and get the first element in PowerShell. As the above -split operator provides an array, to get the first element of the array, you can access the first index [0]:

$cityNames = "New York,Los Angeles,Chicago,Houston,Phoenix"
$cityArray = $cityNames -split ","
$firstCity = $cityArray[0]
Write-Output $firstCity

This will output:

New York

I executed the complete code using VS code, and you can see the output in the screenshot below; it returned the first element from the string.

Split String and Get First Element in PowerShell

Read How to Split a String by Word in PowerShell?

Split String and Get Last Element in PowerShell

In the same way as the above example, we can use the index[-1] to get the last element from the string array. In PowerShell, negative indices count from the end of the array:

$cityNames = "New York,Los Angeles,Chicago,Houston,Phoenix"
$cityArray = $cityNames -split ","
$firstCity = $cityArray[0]
$lastCity = $cityArray[-1]
Write-Output $lastCity

This will output:

Phoenix

Look at the screenshot below; it returns the last element from the string.

powershell split string and get last

Split String and Get the First and Last Element

Sometimes, you might required to get the first and last element of the string in PowerShell.

You can combine the above things.

Here is an example to split a string and get both the first and last elements using PowerShell:

$stateNames = "California,Texas,Florida,New York,Illinois"
$stateArray = $stateNames -split ","
$firstState = $stateArray[0]
$lastState = $stateArray[-1]

Write-Output "First State: $firstState"
Write-Output "Last State: $lastState"

This will output:

First State: California
Last State: Illinois

This will return both the first and last element of the string in PowerShell.

Split String and Remove Last Element in PowerShell

Now, let us check an example of how to split string and remove the last element in PowerShell.

To remove the last element from an array, you can use array slicing. Array slicing in PowerShell can be done using the range operator ... Here’s how you can remove the last element from the array:

$trimmedStateArray = $stateArray[0..($stateArray.Length - 2)]

In this example, 0..($stateArray.Length - 2) creates a range from the first element to the second-to-last element. This effectively removes the last element from the array.

Here is the complete PowerShell script:

$stateNames = "California,Texas,Florida,New York,Illinois"
$stateArray = $stateNames -split ","

# Get the first and last elements
$firstState = $stateArray[0]
$lastState = $stateArray[-1]

Write-Output "First State: $firstState"
Write-Output "Last State: $lastState"

# Remove the last element
$trimmedStateArray = $stateArray[0..($stateArray.Length - 2)]

Write-Output "Array after removing the last element: $($trimmedStateArray -join ",")"

This script will output:

First State: California
Last State: Illinois
Array after removing the last element: California,Texas,Florida,New York

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

powershell split string and remove last

Conclusion

In this PowerShell tutorial, we have explored how to split a string and get the first and last elements. We have also seen how to split a string and remove the last element.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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