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
PhoenixSplit 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 $firstCityThis will output:
New YorkI 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.

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 $lastCityThis will output:
PhoenixLook at the screenshot below; it returns the last element from the string.

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: IllinoisThis 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 YorkYou can see the output in the screenshot below after I executed the PowerShell script using VS code.

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:
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.