Remove first 2 lines from multiple files in a directory

Hello,

Today is my first day playing around with powershell (used to do some scripting in bash/linux and batch scripting way long ago)

I found some scripts that are similar to what I need and am trying to mash them together. I would like to move the first two lines from a number of files that are in a directory but can’t get this to work. Any help would be very appreciated. Thanks!

 

[pre] Get-Content c:\test*.txt | ForEach { Get-Content $.Fullname | Select-Object -Skip 2 | Out-File "$($.Directory)$($_.BaseName)-new.txt"}[/pre]

If you want to overwrite existing files you could start with something like this:

Get-Content c:\test\*.txt | 
    ForEach-Object {
        $Content = Get-Content -Path $_.Fullname
        $Content | 
        Select-Object -Skip 2 | 
        Out-File -FilePath $_.Fullname
    }

Please test with test files! :wink:

Thanks for the quick response. I’m getting the following error

 

 

[pre] Out-File : Cannot bind argument to parameter ‘FilePath’ because it is null.
At C:\test\test4.ps1:6 char:28

  • Out-File -FilePath $_.Fullname
  • CategoryInfo : InvalidData: (:slight_smile: [Out-File], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand

Get-Content : Cannot bind argument to parameter ‘Path’ because it is null.
At C:\test\test4.ps1:3 char:38 [/pre]

<p style=“text-align: center;”></p>
Try this:

Get-Content c:\test*.txt |
ForEach {
Get-Content -Path $_ | Select-Object -Skip 2 | Set-Content $_
}

here $_.FullName will not work. Because returned object is System.String not System.Io.FileInfo

That also did not work, unfortunately. It seems to be spitting out the first line of the file and trying to read it for processing.

[pre] Get-Content : Cannot find drive. A drive with the name 'Job ‘Run_GetDataMonthlyUploadCnts’ ’ does not exist.
At C:\test\test4.ps1:3 char:1

  • Get-Content -Path $_ | Select-Object -Skip 2 | Set-Content $_
  • CategoryInfo : ObjectNotFound: (Job ‘Run_GetDataMonthlyUploadCnts’ :String) [Get-Content], DriveNotFoundException
  • FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand [/pre]

oooops, sorry. My mistake … this should work:

Get-Content c:\test\*.txt | 
    ForEach-Object {
        $FullName = $_.Fullname
        $Content = Get-Content -Path $_.Fullname
        $Content | 
        Select-Object -Skip 2 | 
        Out-File -FilePath $FullName 
    }

Still getting an error. I appreciate you helping to troubleshoot.

 

[pre] Get-Content : Cannot bind argument to parameter ‘Path’ because it is null.
At C:\test\test4.ps1:4 char:38

  • $Content = Get-Content -Path $_.Fullname
  • CategoryInfo : InvalidData: (:slight_smile: [Get-Content], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand [/pre]
# Skip first two lines then output all other lines
Get-ChildItem -Path c:\test -Filter *.txt | ForEach-Object {
    $line = Get-Content -Path $_.FullName
    $line[2..$line.Length] | Out-File "$($_.Directory)\$($_.BaseName)-new.txt"
}

That works, thanks!

 

-og