Search code examples
arrayspowershellsplit

Select all words from string except last (PowerShell)


So what I am trying to achieve is selecting all words from a given string, except the last one. So I have a few strings;

On The Rocks
The Rocks
Major Bananas

I want to select all words, except the last one from every string. I figured out I could use split() to take every word as separate. Though I can't figure it out any further.

Thanks in advance.


Solution

  • Here's how I might do something like this.

    $Sample = "String sample we can use"
    $Split = $Sample.Split(" ")
    [string]$split[0..($Split.count-2)]