Sudhagar Rajaraman
Sudhagar Rajaraman

Reputation: 69

Cut off everything after a specific word - Powershell

I'm trying to remove everything after a word in html file with powershell., But its failing need assistance

$s = get-content "C:\Users\admin\Desktop\File\sheet003.html"

$pos = $s.IndexOf(">0<")
$leftPart = $s.Substring(0, $pos)
#$rightPart = $s.Substring($pos+1)

$leftPart

But getting below error :

Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At line:4 char:1
+ $leftPart = $s.Substring(0, $pos)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Upvotes: 0

Views: 470

Answers (1)

Esperento57
Esperento57

Reputation: 17492

use :

 get-content "C:\temp\sheet003.html" -raw

because otherwise you get an array instead of a string into $s

Upvotes: 2

Related Questions