0

EDIT: So I managed to come up with this code and for the most part it works flawlessly. The problem I am trying to figure out is how to increment $a for each line. I would like an output like this, but I am trying to figure out how to increment $a.

 I 1 like bat scripting! 
 I 2 like vbs scripting! 
 I 3 like powershell scripting! 

$a=0 
$b=  Get-Content -Path 'C:\Users\G\Desktop\2.txt' | ForEach-Object { "I $($a+1) like {0} scripting!" -f $_} 
$(
$b | ForEach {
 $b[$a]=" $($b[$a]) "
    $a++
    
}
$b
) *>&1 > "C:\Users\G\Desktop\3.txt"
Sarash
  • 25
  • 4
  • You context statements to manipulate text files for whatever you choose. Search the web and Youtube for ***PowerShell text file management***' for examples. – postanote May 29 '22 at 08:06
  • 3
    Yes, you might want to use the [`Get-Content`](https://docs.microsoft.com/powershell/module/microsoft.powershell.management/get-content) cmdlet for this: With regards to your example, [do not use the `Write_Host`](https://stackoverflow.com/q/19754069/1701026) in the middle (or start) of a pipeline. Recommend reading for PowerShell: [cmdlet overview](https://docs.microsoft.com/powershell/scripting/developer/cmdlet/cmdlet-overview), [about pipelines](https://docs.microsoft.com/powershell/scripting/developer/cmdlet/cmdlet-overview) – iRon May 29 '22 at 08:13
  • 2
    You mean something like this? `Get-Content -Path 'X:\yourSecondFile.txt' | ForEach-Object { 'I like {0} scripting!' -f $_ }`. Read about [ForEach-Object](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object) and the [Automatic variable `$_`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#_) – Theo May 29 '22 at 09:31
  • Change `$($a+1)` for `$((++$a))` – Santiago Squarzon May 30 '22 at 03:56

0 Answers0