Powershell remove text after first instance of special character Powershell remove text after first instance of special character powershell powershell

Powershell remove text after first instance of special character


$s = '8.2.4.151.65; HBAAPI(I) v1.3; 3-29-02'$s.Substring(0, $s.IndexOf(';'))


Split on the ; and take the first string.

'8.2.4.151.65; HBAAPI(I) v1.3; 3-29-02'.split(';')[0] 


Using a regex with a lazy match:

'8.2.4.151.65; HBAAPI(I) v1.3; 3-29-02' -replace '(.+?);.+','$1'8.2.4.151.65

The ? makes the match 'lazy', so it stop at the first ;