#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

About the author: Aleksandar Nikolic

Aleksandar is a PowerShell MVP and a co-founder of PowerShell Magazine. He is a frequent speaker at the conferences and participates regularly in IT Pro user groups worldwide. Recently, Aleksandar co-authored the highly respected Administrator’s Guide to Windows PowerShell Remoting. Follow him on Twitter at @alexandair

Related Posts