Home Forums Programming Powershell How to copy last line of .txt file into new .txt file? RE: How to copy last line of .txt file into new .txt file?

  • I dont know if I understand vey well,but if you want to get the last line and output to a new file :

    (Get-Content c:\Test\lines.txt )[-1] | Out-File c:\test\NewLine.txt

    Then same wat if you want to get the last 2

    (Get-Content c:\Test\lines.txt )[-1..-2] | Out-File c:\test\NewLine.txt

    or the first 3 (it is adressing to an array and in PowerShell is zero-based, so needs to use 0 to 2)

    (Get-Content c:\Test\lines.txt )[0..2] | Out-File c:\test\NewLine.txt

    or we can use the -totalcount parameter in thecase of the first 3:

    Get-Content c:\Test\lines.txt -totalcount 3 | Out-File c:\test\NewLine.txt

    $hell your Experience !!![/url]