7

#PSTip How to remove the first line from a text file

There are a lot of ways to remove the first line from a text file. I hope you will find a multiple assignment technique interesting. Here is what you need to do:

PS C:\> $a,$b = Get-Content .\test.txt
PS C:\> $b > .\test.txt

The first line of the test.txt file is asigned to the $a variable, the rest of the file goes to the variable $b, and then you write a content of the $b variable to the test.txt file by using > redirection operator.

But wait, there is more! You can turn that into a one-liner:

PS C:\> $a, ${c:test.txt} = Get-Content .\test.txt
Filed in: Columns, Tips and Tricks Tags: , , ,
© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: