I extract some data from my computer and it creates either a variable or I've written it to a .txt file. The output looks like this:
RVR7RYR
RVR7RYR
I I only need the first 7 characters so I wrote this:
$line = get-content "c:\temp\file.txt"
$var = $line
$result = $var.SubString($var.length - 7, 7)
$result
It gives me this error:
Exception calling "Substring" with "2" argument(s): "StartIndex cannot be less than zero. Parameter name: startIndex" At line:5 char:1
- $result = $var.SubString($var.length - 7, 7)
-
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentOutOfRangeException
my file does have spaces inbetween the values and even after the second value, not sure if that matters.
how do I get just the first 7 characters?
echo"$var" and $var.length" to make sure they're really what you think they are. 2) Q: Why not simply$result = $var.SubString(0, 7)?