I'm trying to get the last 8 characters of every line of an array in the pipeline, I thought this would output the last 8 characters but the output seems to be blank
foreach($line in $Texfile)
{
$line[-8..-1]-join ''
}
Try this:
foreach ($Line in $Texfile) {
$Line.Remove(0, ($Line.Length - 8))
}
There's any number of ways to do this, but I opted to pipe Get-Content to ForEach-Object.
Get-Content -Path <Path\to\file.txt> | ForEach-Object {
$_.Substring($_.Length - 8)
}
In your example, you'd use $Line in place of $_ and not pipe to ForEach-Object, and instead, use the Foreach language construct as you've done.
Foreach ($Line in $TextFile) {
$Line.Substring($Line.Length - 8)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With