stringpowershell

How to truncate text in string after/before separator in PowerShell


I want to make a PowerShell script that takes each file of my music library and then does a hash sum of it and writes that into a file like so:

test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198

When I start the script, I need it to first compare my music library with the already existing values, but for this I just want to cut off everything after the ; so that it can compare filename against filename (or filepath)... but I'm stumped at how to do that.

I tried replacing the value via $name = $name -replace ";*","", but that didn't work. I also tried to filter... but I don't know how.


Solution

  • $pos = $name.IndexOf(";")
    $leftPart = $name.Substring(0, $pos)
    $rightPart = $name.Substring($pos+1)
    

    Internally, PowerShell uses the String class.