Take the first 10 characters of each line in the file, from left to right, in Powershell

I would like to take the first number of characters x from the string $ and move them to a variable.

How should I do it?

Here is my existing code

$data = get-content "C:\TestFile.txt"
foreach($line in $data)
{
   if($line.length -gt 250){**get first x amount of characters into variable Y**}
}
+5
source share
1 answer

like this:

$data = get-content "TestFile.txt"
$amount = 10;
$y= @()
foreach($line in $data)
{
   if ( $line.length -gt 250){ $y += $line.substring(0,$amount) } 
}
+7
source

All Articles