0

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

1 Answers1

2

use :

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

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

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Esperento57
  • 16,521
  • 3
  • 39
  • 45