Keep Left Side of Split

Hello Everybody :wink:

My Issue is this :

I have a string like this :

The string after the “_C” can change !

I need to have just “” but with split i dont success to have it.

For the momment i have try some command like :


$Srv = "Master_Server.fomrum.org_123456789_C1_R1.IMG"
$Srv.Split("_")[-1] = R1.img

So i have exactly the Right Side but how to have the left side ?
Can we do the split but begining by the right side and not the Left side to don’ttake car about the name of the server ?
And to get the string “”

Split creates an array of strings. Sounds like you just need part of the original string. I would recommend the -replace operator instead of -split.

$Srv -replace "_R.*", ""

The first value “_R.*” is a regular expression, so modify it as needed for your dataset. I was just working with the example you posted.


Basically you’re trying to remove the last element of an array, which is more painful that it needs to be. Here is a way to do it:

$Srv = "Master_Server.fomrum.org_123456789_C1_R1.IMG"
$temp = ($srv -split '_')[0..$($spl.Count - 2)] -join '_'

After the string is split, we can get index 0 to the array count minus 2. Count starts at 1, not 0 which is -1 and then the last element we want to remote is -1, for a total of 2. Then you glue it back together with underscore.

Another way to do it is with a regular expression. Capture the left and right portions into named capture groups.

$m = [regex]::Match($srv,'(?<left>\d+_C1)_(?<right>.+)')



Hello all ^^


Thx for your reply and Woaw it’s Works fine for this string.

I had finish a Script for a client and they said after the name of the server was not “MasterServer” but “Master_Server…”

In this time i have found a script who works but not all the time :

Function Extract-String {
        , [Parameter(Mandatory=$true)][char]$character
        , [Parameter(Mandatory=$false)][ValidateSet("Right","Left")][string]$range
        , [Parameter(Mandatory=$false)][int]$afternumber
        , [Parameter(Mandatory=$false)][int]$tonumber
        [string]$return = ""

        if ($range -eq "Right")
            $return = $string.Split("$character")[($string.Length - $string.Replace("$character","").Length)]
        elseif ($range -eq "Left")
            $return = $string.Split("$character")[0]
        elseif ($tonumber -ne 0)
            for ($i = $afternumber; $i -le ($afternumber + $tonumber); $i++)
                $return += ($string.Split("$character")[$i]+"$character") #A modifier selon le besoin.
            $return = $string.Split("$character")[$afternumber]

        return $return

Sorry for those who see my first reply, i was too happy by your reply and forget many important things i haven’t speak about, sorry !

The string is this :


"Master_Server.Suffix_C<1..10>_<F | HDR>_<Could have: R><number>.<info | img | fmk>"


Really string before the first split :


So i take the 4 column with the .Split("|")[3] , so i get this :


after i have to take the string to C??? and would like to have it but without take care of the name of the server, if there is an “-” or not.

So i must Keep : “”, but the name of the server could be MasterServerRIE or something else.

I realise, it’s bad that i have done here i put a first thin and after a more tricky things…

I’will work on it with the script you have done.

So sorry !

Any way, Have a nice day Dude :wink:
thx you all :wink:

And i would like to thx you again for all the help i can have on this forums !


And yet another way …

$String = ‘’

$NewString = $String.Substring(1, $String.LastIndexOf(’_’) - 1)

Thx to you ^^

And have a nie Merry Christmas !